Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 1 | //=== 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 Wendling | 0cb8c0b | 2013-08-21 20:36:42 +0000 | [diff] [blame] | 19 | |
Bill Wendling | 0cb8c0b | 2013-08-21 20:36:42 +0000 | [diff] [blame] | 20 | #include "HexagonMachineFunctionInfo.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "HexagonSubtarget.h" |
Eric Christopher | 0120db5 | 2014-05-21 22:42:07 +0000 | [diff] [blame] | 22 | #include "HexagonTargetMachine.h" |
| 23 | #include "HexagonTargetObjectFile.h" |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/LatencyPriorityQueue.h" |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineDominators.h" |
| 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Bill Wendling | 0cb8c0b | 2013-08-21 20:36:42 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bill Wendling | 0cb8c0b | 2013-08-21 20:36:42 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/Passes.h" |
| 31 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Bill Wendling | 0cb8c0b | 2013-08-21 20:36:42 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Bill Wendling | 0cb8c0b | 2013-08-21 20:36:42 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetInstrInfo.h" |
| 37 | #include "llvm/Target/TargetMachine.h" |
| 38 | #include "llvm/Target/TargetRegisterInfo.h" |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "xfer" |
| 43 | |
Colin LeMahieu | 56efafc | 2015-06-15 19:05:35 +0000 | [diff] [blame] | 44 | namespace llvm { |
| 45 | FunctionPass *createHexagonSplitConst32AndConst64(); |
| 46 | void initializeHexagonSplitConst32AndConst64Pass(PassRegistry&); |
| 47 | } |
| 48 | |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | class HexagonSplitConst32AndConst64 : public MachineFunctionPass { |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 52 | public: |
| 53 | static char ID; |
Eric Christopher | 01f875e | 2015-02-02 22:11:43 +0000 | [diff] [blame] | 54 | HexagonSplitConst32AndConst64() : MachineFunctionPass(ID) {} |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 55 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 56 | const char *getPassName() const override { |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 57 | return "Hexagon Split Const32s and Const64s"; |
| 58 | } |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 59 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 60 | MachineFunctionProperties getRequiredProperties() const override { |
| 61 | return MachineFunctionProperties().set( |
| 62 | MachineFunctionProperties::Property::AllVRegsAllocated); |
| 63 | } |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | |
| 67 | char HexagonSplitConst32AndConst64::ID = 0; |
| 68 | |
| 69 | |
| 70 | bool HexagonSplitConst32AndConst64::runOnMachineFunction(MachineFunction &Fn) { |
| 71 | |
Eric Christopher | 0120db5 | 2014-05-21 22:42:07 +0000 | [diff] [blame] | 72 | const HexagonTargetObjectFile &TLOF = |
Eric Christopher | 01f875e | 2015-02-02 22:11:43 +0000 | [diff] [blame] | 73 | *static_cast<const HexagonTargetObjectFile *>( |
| 74 | Fn.getTarget().getObjFileLowering()); |
Krzysztof Parzyszek | 5de5910 | 2016-04-21 18:56:45 +0000 | [diff] [blame] | 75 | if (TLOF.isSmallDataEnabled()) |
Eric Christopher | 0120db5 | 2014-05-21 22:42:07 +0000 | [diff] [blame] | 76 | return true; |
| 77 | |
Eric Christopher | 01f875e | 2015-02-02 22:11:43 +0000 | [diff] [blame] | 78 | const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo(); |
Colin LeMahieu | 5425109 | 2015-03-09 20:11:02 +0000 | [diff] [blame] | 79 | const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo(); |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 80 | |
| 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 Smith | a72c6e2 | 2015-10-20 00:46:39 +0000 | [diff] [blame] | 84 | MachineBasicBlock *MBB = &*MBBb; |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 85 | // Traverse the basic block |
| 86 | MachineBasicBlock::iterator MII = MBB->begin(); |
| 87 | MachineBasicBlock::iterator MIE = MBB->end (); |
| 88 | while (MII != MIE) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 89 | MachineInstr &MI = *MII; |
| 90 | int Opc = MI.getOpcode(); |
Krzysztof Parzyszek | cd97c98 | 2015-04-22 18:25:53 +0000 | [diff] [blame] | 91 | if (Opc == Hexagon::CONST32_Int_Real && |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 92 | MI.getOperand(1).isBlockAddress()) { |
| 93 | int DestReg = MI.getOperand(0).getReg(); |
| 94 | MachineOperand &Symbol = MI.getOperand(1); |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 95 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 96 | BuildMI(*MBB, MII, MI.getDebugLoc(), TII->get(Hexagon::LO), DestReg) |
| 97 | .addOperand(Symbol); |
| 98 | BuildMI(*MBB, MII, MI.getDebugLoc(), TII->get(Hexagon::HI), DestReg) |
| 99 | .addOperand(Symbol); |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 100 | // MBB->erase returns the iterator to the next instruction, which is the |
| 101 | // one we want to process next |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 102 | MII = MBB->erase(&MI); |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 103 | continue; |
| 104 | } |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 105 | |
Colin LeMahieu | 5425109 | 2015-03-09 20:11:02 +0000 | [diff] [blame] | 106 | else if (Opc == Hexagon::CONST32_Int_Real || |
| 107 | Opc == Hexagon::CONST32_Float_Real) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 108 | int DestReg = MI.getOperand(0).getReg(); |
Colin LeMahieu | 5425109 | 2015-03-09 20:11:02 +0000 | [diff] [blame] | 109 | |
| 110 | // We have to convert an FP immediate into its corresponding integer |
| 111 | // representation |
| 112 | int64_t ImmValue; |
| 113 | if (Opc == Hexagon::CONST32_Float_Real) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 114 | APFloat Val = MI.getOperand(1).getFPImm()->getValueAPF(); |
Colin LeMahieu | 5425109 | 2015-03-09 20:11:02 +0000 | [diff] [blame] | 115 | ImmValue = *Val.bitcastToAPInt().getRawData(); |
| 116 | } |
| 117 | else |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 118 | ImmValue = MI.getOperand(1).getImm(); |
Colin LeMahieu | 5425109 | 2015-03-09 20:11:02 +0000 | [diff] [blame] | 119 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 120 | BuildMI(*MBB, MII, MI.getDebugLoc(), TII->get(Hexagon::A2_tfrsi), |
| 121 | DestReg) |
| 122 | .addImm(ImmValue); |
| 123 | MII = MBB->erase(&MI); |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 124 | continue; |
| 125 | } |
Colin LeMahieu | 5425109 | 2015-03-09 20:11:02 +0000 | [diff] [blame] | 126 | else if (Opc == Hexagon::CONST64_Int_Real || |
| 127 | Opc == Hexagon::CONST64_Float_Real) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 128 | int DestReg = MI.getOperand(0).getReg(); |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 129 | |
Colin LeMahieu | 5425109 | 2015-03-09 20:11:02 +0000 | [diff] [blame] | 130 | // We have to convert an FP immediate into its corresponding integer |
| 131 | // representation |
| 132 | int64_t ImmValue; |
| 133 | if (Opc == Hexagon::CONST64_Float_Real) { |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 134 | APFloat Val = MI.getOperand(1).getFPImm()->getValueAPF(); |
Colin LeMahieu | 5425109 | 2015-03-09 20:11:02 +0000 | [diff] [blame] | 135 | ImmValue = *Val.bitcastToAPInt().getRawData(); |
| 136 | } |
| 137 | else |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 138 | ImmValue = MI.getOperand(1).getImm(); |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 139 | |
Colin LeMahieu | 5425109 | 2015-03-09 20:11:02 +0000 | [diff] [blame] | 140 | unsigned DestLo = TRI->getSubReg(DestReg, Hexagon::subreg_loreg); |
| 141 | unsigned DestHi = TRI->getSubReg(DestReg, Hexagon::subreg_hireg); |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 142 | |
| 143 | int32_t LowWord = (ImmValue & 0xFFFFFFFF); |
| 144 | int32_t HighWord = (ImmValue >> 32) & 0xFFFFFFFF; |
| 145 | |
Duncan P. N. Exon Smith | 98226e3 | 2016-07-12 01:55:32 +0000 | [diff] [blame^] | 146 | BuildMI(*MBB, MII, MI.getDebugLoc(), TII->get(Hexagon::A2_tfrsi), |
| 147 | DestLo) |
| 148 | .addImm(LowWord); |
| 149 | BuildMI(*MBB, MII, MI.getDebugLoc(), TII->get(Hexagon::A2_tfrsi), |
| 150 | DestHi) |
| 151 | .addImm(HighWord); |
| 152 | MII = MBB->erase(&MI); |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 153 | continue; |
Colin LeMahieu | 5425109 | 2015-03-09 20:11:02 +0000 | [diff] [blame] | 154 | } |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 155 | ++MII; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 162 | } |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 163 | |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | // Public Constructor Functions |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | |
| 168 | FunctionPass * |
Eric Christopher | 01f875e | 2015-02-02 22:11:43 +0000 | [diff] [blame] | 169 | llvm::createHexagonSplitConst32AndConst64() { |
| 170 | return new HexagonSplitConst32AndConst64(); |
Jyotsna Verma | 5eb5980 | 2013-05-07 19:53:00 +0000 | [diff] [blame] | 171 | } |