Misha Brukman | 2a8350a | 2005-02-05 02:24:26 +0000 | [diff] [blame] | 1 | //===- AlphaInstrInfo.cpp - Alpha Instruction Information -------*- C++ -*-===// |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 2 | // |
Andrew Lenharth | 304d0f3 | 2005-01-22 23:41:55 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 7 | // |
Andrew Lenharth | 304d0f3 | 2005-01-22 23:41:55 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the Alpha implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Alpha.h" |
| 15 | #include "AlphaInstrInfo.h" |
| 16 | #include "AlphaGenInstrInfo.inc" |
| 17 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Andrew Lenharth | 304d0f3 | 2005-01-22 23:41:55 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
| 20 | AlphaInstrInfo::AlphaInstrInfo() |
Evan Cheng | 7ce4578 | 2006-11-13 23:36:35 +0000 | [diff] [blame] | 21 | : TargetInstrInfo(AlphaInsts, sizeof(AlphaInsts)/sizeof(AlphaInsts[0])), |
| 22 | RI(*this) { } |
Andrew Lenharth | 304d0f3 | 2005-01-22 23:41:55 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI, |
| 26 | unsigned& sourceReg, |
| 27 | unsigned& destReg) const { |
Andrew Lenharth | 304d0f3 | 2005-01-22 23:41:55 +0000 | [diff] [blame] | 28 | MachineOpCode oc = MI.getOpcode(); |
Andrew Lenharth | 6bbf6b0 | 2006-10-31 23:46:56 +0000 | [diff] [blame] | 29 | if (oc == Alpha::BISr || |
Andrew Lenharth | ddc877c | 2006-03-09 18:18:51 +0000 | [diff] [blame] | 30 | oc == Alpha::CPYSS || |
| 31 | oc == Alpha::CPYST || |
| 32 | oc == Alpha::CPYSSt || |
| 33 | oc == Alpha::CPYSTs) { |
Andrew Lenharth | 5cefc5e | 2005-11-09 19:17:08 +0000 | [diff] [blame] | 34 | // or r1, r2, r2 |
| 35 | // cpys(s|t) r1 r2 r2 |
Andrew Lenharth | 304d0f3 | 2005-01-22 23:41:55 +0000 | [diff] [blame] | 36 | assert(MI.getNumOperands() == 3 && |
| 37 | MI.getOperand(0).isRegister() && |
| 38 | MI.getOperand(1).isRegister() && |
| 39 | MI.getOperand(2).isRegister() && |
| 40 | "invalid Alpha BIS instruction!"); |
| 41 | if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) { |
| 42 | sourceReg = MI.getOperand(1).getReg(); |
| 43 | destReg = MI.getOperand(0).getReg(); |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | return false; |
| 48 | } |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 49 | |
| 50 | unsigned |
| 51 | AlphaInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const { |
| 52 | switch (MI->getOpcode()) { |
| 53 | case Alpha::LDL: |
| 54 | case Alpha::LDQ: |
| 55 | case Alpha::LDBU: |
| 56 | case Alpha::LDWU: |
| 57 | case Alpha::LDS: |
| 58 | case Alpha::LDT: |
| 59 | if (MI->getOperand(1).isFrameIndex()) { |
| 60 | FrameIndex = MI->getOperand(1).getFrameIndex(); |
| 61 | return MI->getOperand(0).getReg(); |
| 62 | } |
| 63 | break; |
| 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
Andrew Lenharth | 133d310 | 2006-02-03 03:07:37 +0000 | [diff] [blame] | 68 | unsigned |
| 69 | AlphaInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const { |
| 70 | switch (MI->getOpcode()) { |
| 71 | case Alpha::STL: |
| 72 | case Alpha::STQ: |
| 73 | case Alpha::STB: |
| 74 | case Alpha::STW: |
| 75 | case Alpha::STS: |
| 76 | case Alpha::STT: |
| 77 | if (MI->getOperand(1).isFrameIndex()) { |
| 78 | FrameIndex = MI->getOperand(1).getFrameIndex(); |
| 79 | return MI->getOperand(0).getReg(); |
| 80 | } |
| 81 | break; |
| 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | |
Andrew Lenharth | f81173f | 2006-10-31 16:49:55 +0000 | [diff] [blame] | 86 | static bool isAlphaIntCondCode(unsigned Opcode) { |
| 87 | switch (Opcode) { |
| 88 | case Alpha::BEQ: |
| 89 | case Alpha::BNE: |
| 90 | case Alpha::BGE: |
| 91 | case Alpha::BGT: |
| 92 | case Alpha::BLE: |
| 93 | case Alpha::BLT: |
| 94 | case Alpha::BLBC: |
| 95 | case Alpha::BLBS: |
| 96 | return true; |
| 97 | default: |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | |
Chris Lattner | 0476b28 | 2006-10-24 16:41:36 +0000 | [diff] [blame] | 102 | void AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB, |
| 103 | MachineBasicBlock *FBB, |
| 104 | const std::vector<MachineOperand> &Cond)const{ |
Andrew Lenharth | f81173f | 2006-10-31 16:49:55 +0000 | [diff] [blame] | 105 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
| 106 | assert((Cond.size() == 2 || Cond.size() == 0) && |
| 107 | "Alpha branch conditions have two components!"); |
| 108 | |
| 109 | // One-way branch. |
| 110 | if (FBB == 0) { |
| 111 | if (Cond.empty()) // Unconditional branch |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 112 | BuildMI(&MBB, get(Alpha::BR)).addMBB(TBB); |
Andrew Lenharth | f81173f | 2006-10-31 16:49:55 +0000 | [diff] [blame] | 113 | else // Conditional branch |
| 114 | if (isAlphaIntCondCode(Cond[0].getImm())) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 115 | BuildMI(&MBB, get(Alpha::COND_BRANCH_I)) |
Andrew Lenharth | f81173f | 2006-10-31 16:49:55 +0000 | [diff] [blame] | 116 | .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); |
| 117 | else |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 118 | BuildMI(&MBB, get(Alpha::COND_BRANCH_F)) |
Andrew Lenharth | f81173f | 2006-10-31 16:49:55 +0000 | [diff] [blame] | 119 | .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | // Two-way Conditional Branch. |
| 124 | if (isAlphaIntCondCode(Cond[0].getImm())) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 125 | BuildMI(&MBB, get(Alpha::COND_BRANCH_I)) |
Andrew Lenharth | f81173f | 2006-10-31 16:49:55 +0000 | [diff] [blame] | 126 | .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); |
| 127 | else |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 128 | BuildMI(&MBB, get(Alpha::COND_BRANCH_F)) |
Andrew Lenharth | f81173f | 2006-10-31 16:49:55 +0000 | [diff] [blame] | 129 | .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 130 | BuildMI(&MBB, get(Alpha::BR)).addMBB(FBB); |
Rafael Espindola | 3d7d39a | 2006-10-24 17:07:11 +0000 | [diff] [blame] | 131 | } |
Andrew Lenharth | f81173f | 2006-10-31 16:49:55 +0000 | [diff] [blame] | 132 | |
| 133 | static unsigned AlphaRevCondCode(unsigned Opcode) { |
| 134 | switch (Opcode) { |
| 135 | case Alpha::BEQ: return Alpha::BNE; |
| 136 | case Alpha::BNE: return Alpha::BEQ; |
| 137 | case Alpha::BGE: return Alpha::BLT; |
| 138 | case Alpha::BGT: return Alpha::BLE; |
| 139 | case Alpha::BLE: return Alpha::BGT; |
| 140 | case Alpha::BLT: return Alpha::BGE; |
| 141 | case Alpha::BLBC: return Alpha::BLBS; |
| 142 | case Alpha::BLBS: return Alpha::BLBC; |
| 143 | case Alpha::FBEQ: return Alpha::FBNE; |
| 144 | case Alpha::FBNE: return Alpha::FBEQ; |
| 145 | case Alpha::FBGE: return Alpha::FBLT; |
| 146 | case Alpha::FBGT: return Alpha::FBLE; |
| 147 | case Alpha::FBLE: return Alpha::FBGT; |
| 148 | case Alpha::FBLT: return Alpha::FBGE; |
| 149 | default: |
| 150 | assert(0 && "Unknown opcode"); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Branch analysis. |
| 155 | bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, |
| 156 | MachineBasicBlock *&FBB, |
| 157 | std::vector<MachineOperand> &Cond) const { |
| 158 | // If the block has no terminators, it just falls into the block after it. |
| 159 | MachineBasicBlock::iterator I = MBB.end(); |
| 160 | if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) |
| 161 | return false; |
| 162 | |
| 163 | // Get the last instruction in the block. |
| 164 | MachineInstr *LastInst = I; |
| 165 | |
| 166 | // If there is only one terminator instruction, process it. |
| 167 | if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) { |
| 168 | if (LastInst->getOpcode() == Alpha::BR) { |
| 169 | TBB = LastInst->getOperand(0).getMachineBasicBlock(); |
| 170 | return false; |
| 171 | } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I || |
| 172 | LastInst->getOpcode() == Alpha::COND_BRANCH_F) { |
| 173 | // Block ends with fall-through condbranch. |
| 174 | TBB = LastInst->getOperand(2).getMachineBasicBlock(); |
| 175 | Cond.push_back(LastInst->getOperand(0)); |
| 176 | Cond.push_back(LastInst->getOperand(1)); |
| 177 | return false; |
| 178 | } |
| 179 | // Otherwise, don't know what this is. |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | // Get the instruction before it if it's a terminator. |
| 184 | MachineInstr *SecondLastInst = I; |
| 185 | |
| 186 | // If there are three terminators, we don't know what sort of block this is. |
| 187 | if (SecondLastInst && I != MBB.begin() && |
| 188 | isTerminatorInstr((--I)->getOpcode())) |
| 189 | return true; |
| 190 | |
| 191 | // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it. |
| 192 | if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I || |
| 193 | SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) && |
| 194 | LastInst->getOpcode() == Alpha::BR) { |
| 195 | TBB = SecondLastInst->getOperand(2).getMachineBasicBlock(); |
| 196 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 197 | Cond.push_back(SecondLastInst->getOperand(1)); |
| 198 | FBB = LastInst->getOperand(0).getMachineBasicBlock(); |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | // Otherwise, can't handle this. |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | void AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
| 207 | MachineBasicBlock::iterator I = MBB.end(); |
| 208 | if (I == MBB.begin()) return; |
| 209 | --I; |
| 210 | if (I->getOpcode() != Alpha::BR && |
| 211 | I->getOpcode() != Alpha::COND_BRANCH_I && |
| 212 | I->getOpcode() != Alpha::COND_BRANCH_F) |
| 213 | return; |
| 214 | |
| 215 | // Remove the branch. |
| 216 | I->eraseFromParent(); |
| 217 | |
| 218 | I = MBB.end(); |
| 219 | |
| 220 | if (I == MBB.begin()) return; |
| 221 | --I; |
| 222 | if (I->getOpcode() != Alpha::COND_BRANCH_I && |
| 223 | I->getOpcode() != Alpha::COND_BRANCH_F) |
| 224 | return; |
| 225 | |
| 226 | // Remove the branch. |
| 227 | I->eraseFromParent(); |
| 228 | } |
| 229 | |
| 230 | void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB, |
| 231 | MachineBasicBlock::iterator MI) const { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 232 | BuildMI(MBB, MI, get(Alpha::BISr), Alpha::R31).addReg(Alpha::R31) |
Andrew Lenharth | f81173f | 2006-10-31 16:49:55 +0000 | [diff] [blame] | 233 | .addReg(Alpha::R31); |
| 234 | } |
| 235 | |
| 236 | bool AlphaInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const { |
| 237 | if (MBB.empty()) return false; |
| 238 | |
| 239 | switch (MBB.back().getOpcode()) { |
| 240 | case Alpha::BR: // Uncond branch. |
| 241 | case Alpha::JMP: // Indirect branch. |
| 242 | return true; |
| 243 | default: return false; |
| 244 | } |
| 245 | } |
| 246 | bool AlphaInstrInfo:: |
| 247 | ReverseBranchCondition(std::vector<MachineOperand> &Cond) const { |
| 248 | assert(Cond.size() == 2 && "Invalid Alpha branch opcode!"); |
| 249 | Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm())); |
| 250 | return false; |
| 251 | } |
| 252 | |