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