blob: 9503d4498161c547a8edbcdfc9a83a14bc8446bf [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
Colin LeMahieu56efafc2015-06-15 19:05:35 +000048namespace llvm {
49 FunctionPass *createHexagonSplitConst32AndConst64();
50 void initializeHexagonSplitConst32AndConst64Pass(PassRegistry&);
51}
52
Jyotsna Verma5eb59802013-05-07 19:53:00 +000053namespace {
54
55class HexagonSplitConst32AndConst64 : public MachineFunctionPass {
Jyotsna Verma5eb59802013-05-07 19:53:00 +000056 public:
57 static char ID;
Eric Christopher01f875e2015-02-02 22:11:43 +000058 HexagonSplitConst32AndConst64() : MachineFunctionPass(ID) {}
Jyotsna Verma5eb59802013-05-07 19:53:00 +000059
Craig Topper906c2cd2014-04-29 07:58:16 +000060 const char *getPassName() const override {
Jyotsna Verma5eb59802013-05-07 19:53:00 +000061 return "Hexagon Split Const32s and Const64s";
62 }
Craig Topper906c2cd2014-04-29 07:58:16 +000063 bool runOnMachineFunction(MachineFunction &Fn) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000064 MachineFunctionProperties getRequiredProperties() const override {
65 return MachineFunctionProperties().set(
66 MachineFunctionProperties::Property::AllVRegsAllocated);
67 }
Jyotsna Verma5eb59802013-05-07 19:53:00 +000068};
69
70
71char HexagonSplitConst32AndConst64::ID = 0;
72
73
74bool HexagonSplitConst32AndConst64::runOnMachineFunction(MachineFunction &Fn) {
75
Eric Christopher0120db52014-05-21 22:42:07 +000076 const HexagonTargetObjectFile &TLOF =
Eric Christopher01f875e2015-02-02 22:11:43 +000077 *static_cast<const HexagonTargetObjectFile *>(
78 Fn.getTarget().getObjFileLowering());
Eric Christopher0120db52014-05-21 22:42:07 +000079 if (TLOF.IsSmallDataEnabled())
80 return true;
81
Eric Christopher01f875e2015-02-02 22:11:43 +000082 const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
Colin LeMahieu54251092015-03-09 20:11:02 +000083 const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
Jyotsna Verma5eb59802013-05-07 19:53:00 +000084
85 // Loop over all of the basic blocks
86 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
87 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +000088 MachineBasicBlock *MBB = &*MBBb;
Jyotsna Verma5eb59802013-05-07 19:53:00 +000089 // Traverse the basic block
90 MachineBasicBlock::iterator MII = MBB->begin();
91 MachineBasicBlock::iterator MIE = MBB->end ();
92 while (MII != MIE) {
93 MachineInstr *MI = MII;
94 int Opc = MI->getOpcode();
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +000095 if (Opc == Hexagon::CONST32_Int_Real &&
96 MI->getOperand(1).isBlockAddress()) {
Jyotsna Verma5eb59802013-05-07 19:53:00 +000097 int DestReg = MI->getOperand(0).getReg();
98 MachineOperand &Symbol = MI->getOperand (1);
99
100 BuildMI (*MBB, MII, MI->getDebugLoc(),
101 TII->get(Hexagon::LO), DestReg).addOperand(Symbol);
102 BuildMI (*MBB, MII, MI->getDebugLoc(),
103 TII->get(Hexagon::HI), DestReg).addOperand(Symbol);
104 // MBB->erase returns the iterator to the next instruction, which is the
105 // one we want to process next
106 MII = MBB->erase (MI);
107 continue;
108 }
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000109
Colin LeMahieu54251092015-03-09 20:11:02 +0000110 else if (Opc == Hexagon::CONST32_Int_Real ||
111 Opc == Hexagon::CONST32_Float_Real) {
112 int DestReg = MI->getOperand(0).getReg();
113
114 // We have to convert an FP immediate into its corresponding integer
115 // representation
116 int64_t ImmValue;
117 if (Opc == Hexagon::CONST32_Float_Real) {
118 APFloat Val = MI->getOperand(1).getFPImm()->getValueAPF();
119 ImmValue = *Val.bitcastToAPInt().getRawData();
120 }
121 else
122 ImmValue = MI->getOperand(1).getImm();
123
124 BuildMI(*MBB, MII, MI->getDebugLoc(),
125 TII->get(Hexagon::A2_tfrsi), DestReg).addImm(ImmValue);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000126 MII = MBB->erase (MI);
127 continue;
128 }
Colin LeMahieu54251092015-03-09 20:11:02 +0000129 else if (Opc == Hexagon::CONST64_Int_Real ||
130 Opc == Hexagon::CONST64_Float_Real) {
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000131 int DestReg = MI->getOperand(0).getReg();
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000132
Colin LeMahieu54251092015-03-09 20:11:02 +0000133 // We have to convert an FP immediate into its corresponding integer
134 // representation
135 int64_t ImmValue;
136 if (Opc == Hexagon::CONST64_Float_Real) {
137 APFloat Val = MI->getOperand(1).getFPImm()->getValueAPF();
138 ImmValue = *Val.bitcastToAPInt().getRawData();
139 }
140 else
141 ImmValue = MI->getOperand(1).getImm();
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000142
Colin LeMahieu54251092015-03-09 20:11:02 +0000143 unsigned DestLo = TRI->getSubReg(DestReg, Hexagon::subreg_loreg);
144 unsigned DestHi = TRI->getSubReg(DestReg, Hexagon::subreg_hireg);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000145
146 int32_t LowWord = (ImmValue & 0xFFFFFFFF);
147 int32_t HighWord = (ImmValue >> 32) & 0xFFFFFFFF;
148
Colin LeMahieu54251092015-03-09 20:11:02 +0000149 BuildMI(*MBB, MII, MI->getDebugLoc(),
150 TII->get(Hexagon::A2_tfrsi), DestLo).addImm(LowWord);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000151 BuildMI (*MBB, MII, MI->getDebugLoc(),
Colin LeMahieu54251092015-03-09 20:11:02 +0000152 TII->get(Hexagon::A2_tfrsi), DestHi).addImm(HighWord);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000153 MII = MBB->erase (MI);
154 continue;
Colin LeMahieu54251092015-03-09 20:11:02 +0000155 }
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000156 ++MII;
157 }
158 }
159
160 return true;
161}
162
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000163}
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000164
165//===----------------------------------------------------------------------===//
166// Public Constructor Functions
167//===----------------------------------------------------------------------===//
168
169FunctionPass *
Eric Christopher01f875e2015-02-02 22:11:43 +0000170llvm::createHexagonSplitConst32AndConst64() {
171 return new HexagonSplitConst32AndConst64();
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000172}