Tony Linthicum | b4b5415 | 2011-12-12 21:14:40 +0000 | [diff] [blame^] | 1 | //===---- HexagonSplitTFRCondSets.cpp - split TFR condsets into xfers -----===// |
| 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 | // This pass tries to provide opportunities for better optimization of muxes. |
| 11 | // The default code generated for something like: flag = (a == b) ? 1 : 3; |
| 12 | // would be: |
| 13 | // |
| 14 | // {p0 = cmp.eq(r0,r1)} |
| 15 | // {r3 = mux(p0,#1,#3)} |
| 16 | // |
| 17 | // This requires two packets. If we use .new predicated immediate transfers, |
| 18 | // then we can do this in a single packet, e.g.: |
| 19 | // |
| 20 | // {p0 = cmp.eq(r0,r1) |
| 21 | // if (p0.new) r3 = #1 |
| 22 | // if (!p0.new) r3 = #3} |
| 23 | // |
| 24 | // Note that the conditional assignments are not generated in .new form here. |
| 25 | // We assume opptimisically that they will be formed later. |
| 26 | // |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | #define DEBUG_TYPE "xfer" |
| 30 | #include "llvm/CodeGen/Passes.h" |
| 31 | #include "llvm/CodeGen/LatencyPriorityQueue.h" |
| 32 | #include "llvm/CodeGen/SchedulerRegistry.h" |
| 33 | #include "llvm/CodeGen/MachineDominators.h" |
| 34 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 35 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 36 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 37 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
| 38 | #include "llvm/Target/TargetMachine.h" |
| 39 | #include "llvm/Target/TargetInstrInfo.h" |
| 40 | #include "llvm/Target/TargetRegisterInfo.h" |
| 41 | #include "llvm/Support/Compiler.h" |
| 42 | #include "llvm/Support/Debug.h" |
| 43 | #include "llvm/ADT/Statistic.h" |
| 44 | #include "llvm/Support/MathExtras.h" |
| 45 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 46 | #include "HexagonTargetMachine.h" |
| 47 | #include "HexagonSubtarget.h" |
| 48 | #include "HexagonMachineFunctionInfo.h" |
| 49 | #include <map> |
| 50 | #include <iostream> |
| 51 | |
| 52 | #include "llvm/Support/CommandLine.h" |
| 53 | #define DEBUG_TYPE "xfer" |
| 54 | |
| 55 | |
| 56 | using namespace llvm; |
| 57 | |
| 58 | namespace { |
| 59 | |
| 60 | class HexagonSplitTFRCondSets : public MachineFunctionPass { |
| 61 | HexagonTargetMachine& QTM; |
| 62 | const HexagonSubtarget &QST; |
| 63 | |
| 64 | public: |
| 65 | static char ID; |
| 66 | HexagonSplitTFRCondSets(HexagonTargetMachine& TM) : |
| 67 | MachineFunctionPass(ID), QTM(TM), QST(*TM.getSubtargetImpl()) {} |
| 68 | |
| 69 | const char *getPassName() const { |
| 70 | return "Hexagon Split TFRCondSets"; |
| 71 | } |
| 72 | bool runOnMachineFunction(MachineFunction &Fn); |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | char HexagonSplitTFRCondSets::ID = 0; |
| 77 | |
| 78 | |
| 79 | bool HexagonSplitTFRCondSets::runOnMachineFunction(MachineFunction &Fn) { |
| 80 | |
| 81 | const TargetInstrInfo *TII = QTM.getInstrInfo(); |
| 82 | |
| 83 | // Loop over all of the basic blocks. |
| 84 | for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end(); |
| 85 | MBBb != MBBe; ++MBBb) { |
| 86 | MachineBasicBlock* MBB = MBBb; |
| 87 | // Traverse the basic block. |
| 88 | for (MachineBasicBlock::iterator MII = MBB->begin(); MII != MBB->end(); |
| 89 | ++MII) { |
| 90 | MachineInstr *MI = MII; |
| 91 | int Opc = MI->getOpcode(); |
| 92 | if (Opc == Hexagon::TFR_condset_rr) { |
| 93 | |
| 94 | int DestReg = MI->getOperand(0).getReg(); |
| 95 | int SrcReg1 = MI->getOperand(2).getReg(); |
| 96 | int SrcReg2 = MI->getOperand(3).getReg(); |
| 97 | |
| 98 | // Minor optimization: do not emit the predicated copy if the source and |
| 99 | // the destination is the same register |
| 100 | if (DestReg != SrcReg1) { |
| 101 | BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_cPt), |
| 102 | DestReg).addReg(MI->getOperand(1).getReg()).addReg(SrcReg1); |
| 103 | } |
| 104 | if (DestReg != SrcReg2) { |
| 105 | BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_cNotPt), |
| 106 | DestReg).addReg(MI->getOperand(1).getReg()).addReg(SrcReg2); |
| 107 | } |
| 108 | MII = MBB->erase(MI); |
| 109 | --MII; |
| 110 | } else if (Opc == Hexagon::TFR_condset_ii) { |
| 111 | int DestReg = MI->getOperand(0).getReg(); |
| 112 | int SrcReg1 = MI->getOperand(1).getReg(); |
| 113 | int Immed1 = MI->getOperand(2).getImm(); |
| 114 | int Immed2 = MI->getOperand(3).getImm(); |
| 115 | BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFRI_cPt), |
| 116 | DestReg).addReg(SrcReg1).addImm(Immed1); |
| 117 | BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFRI_cNotPt), |
| 118 | DestReg).addReg(SrcReg1).addImm(Immed2); |
| 119 | MII = MBB->erase(MI); |
| 120 | --MII; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | //===----------------------------------------------------------------------===// |
| 131 | // Public Constructor Functions |
| 132 | //===----------------------------------------------------------------------===// |
| 133 | |
| 134 | FunctionPass *llvm::createHexagonSplitTFRCondSets(HexagonTargetMachine &TM) { |
| 135 | return new HexagonSplitTFRCondSets(TM); |
| 136 | } |