Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 1 | //===- SSEDomainFix.cpp - Use proper int/float domain for SSE ---*- C++ -*-===// |
| 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 file contains the SSEDomainFix pass. |
| 11 | // |
| 12 | // Some SSE instructions like mov, and, or, xor are available in different |
| 13 | // variants for different operand types. These variant instructions are |
| 14 | // equivalent, but on Nehalem and newer cpus there is extra latency |
| 15 | // transferring data between integer and floating point domains. |
| 16 | // |
| 17 | // This pass changes the variant instructions to minimize domain crossings. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #define DEBUG_TYPE "sse-domain-fix" |
| 22 | #include "X86InstrInfo.h" |
| 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 25 | #include "llvm/ADT/DepthFirstIterator.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 31 | |
| 32 | /// Allocate objects from a pool, allow objects to be recycled, and provide a |
| 33 | /// way of deleting everything. |
| 34 | template<typename T, unsigned PageSize = 64> |
| 35 | class PoolAllocator { |
| 36 | std::vector<T*> Pages, Avail; |
| 37 | public: |
| 38 | ~PoolAllocator() { Clear(); } |
| 39 | |
| 40 | T* Alloc() { |
| 41 | if (Avail.empty()) { |
| 42 | T *p = new T[PageSize]; |
| 43 | Pages.push_back(p); |
| 44 | Avail.reserve(PageSize); |
| 45 | for (unsigned n = 0; n != PageSize; ++n) |
| 46 | Avail.push_back(p+n); |
| 47 | } |
| 48 | T *p = Avail.back(); |
| 49 | Avail.pop_back(); |
| 50 | return p; |
| 51 | } |
| 52 | |
| 53 | // Allow object to be reallocated. It won't be reconstructed. |
| 54 | void Recycle(T *p) { |
| 55 | p->clear(); |
| 56 | Avail.push_back(p); |
| 57 | } |
| 58 | |
| 59 | // Destroy all objects, make sure there are no external pointers to them. |
| 60 | void Clear() { |
| 61 | Avail.clear(); |
| 62 | while (!Pages.empty()) { |
| 63 | delete[] Pages.back(); |
| 64 | Pages.pop_back(); |
| 65 | } |
| 66 | } |
| 67 | }; |
| 68 | |
Chris Lattner | 563d83f | 2010-03-31 20:32:51 +0000 | [diff] [blame^] | 69 | /// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 70 | /// of execution domains. |
| 71 | /// |
| 72 | /// An open DomainValue represents a set of instructions that can still switch |
| 73 | /// execution domain. Multiple registers may refer to the same open |
| 74 | /// DomainValue - they will eventually be collapsed to the same execution |
| 75 | /// domain. |
| 76 | /// |
| 77 | /// A collapsed DomainValue represents a single register that has been forced |
| 78 | /// into one of more execution domains. There is a separate collapsed |
| 79 | /// DomainValue for each register, but it may contain multiple execution |
| 80 | /// domains. A register value is initially created in a single execution |
| 81 | /// domain, but if we were forced to pay the penalty of a domain crossing, we |
| 82 | /// keep track of the fact the the register is now available in multiple |
| 83 | /// domains. |
| 84 | struct DomainValue { |
| 85 | // Basic reference counting. |
| 86 | unsigned Refs; |
| 87 | |
| 88 | // Available domains. For an open DomainValue, it is the still possible |
| 89 | // domains for collapsing. For a collapsed DomainValue it is the domains where |
| 90 | // the register is available for free. |
| 91 | unsigned Mask; |
| 92 | |
| 93 | // Position of the last defining instruction. |
| 94 | unsigned Dist; |
| 95 | |
| 96 | // Twiddleable instructions using or defining these registers. |
| 97 | SmallVector<MachineInstr*, 8> Instrs; |
| 98 | |
| 99 | // Collapsed DomainValue have no instructions to twiddle - it simply keeps |
| 100 | // track of the domains where the registers are already available. |
| 101 | bool collapsed() const { return Instrs.empty(); } |
| 102 | |
| 103 | // Is any domain in mask available? |
| 104 | bool compat(unsigned mask) const { |
| 105 | return Mask & mask; |
| 106 | } |
| 107 | |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 108 | // Mark domain as available. |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 109 | void add(unsigned domain) { |
| 110 | Mask |= 1u << domain; |
| 111 | } |
| 112 | |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 113 | // First domain available in mask. |
| 114 | unsigned firstDomain() const { |
| 115 | return CountTrailingZeros_32(Mask); |
| 116 | } |
| 117 | |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 118 | DomainValue() { clear(); } |
| 119 | |
| 120 | void clear() { |
| 121 | Refs = Mask = Dist = 0; |
| 122 | Instrs.clear(); |
| 123 | } |
| 124 | }; |
| 125 | |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 126 | static const unsigned NumRegs = 16; |
| 127 | |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 128 | class SSEDomainFixPass : public MachineFunctionPass { |
| 129 | static char ID; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 130 | PoolAllocator<DomainValue> Pool; |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 131 | |
| 132 | MachineFunction *MF; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 133 | const X86InstrInfo *TII; |
| 134 | const TargetRegisterInfo *TRI; |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 135 | MachineBasicBlock *MBB; |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 136 | DomainValue **LiveRegs; |
| 137 | typedef DenseMap<MachineBasicBlock*,DomainValue**> LiveOutMap; |
| 138 | LiveOutMap LiveOuts; |
| 139 | unsigned Distance; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 140 | |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 141 | public: |
| 142 | SSEDomainFixPass() : MachineFunctionPass(&ID) {} |
| 143 | |
| 144 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 145 | AU.setPreservesAll(); |
| 146 | MachineFunctionPass::getAnalysisUsage(AU); |
| 147 | } |
| 148 | |
| 149 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 150 | |
| 151 | virtual const char *getPassName() const { |
| 152 | return "SSE execution domain fixup"; |
| 153 | } |
| 154 | |
| 155 | private: |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 156 | // Register mapping. |
| 157 | int RegIndex(unsigned Reg); |
| 158 | |
| 159 | // LiveRegs manipulations. |
| 160 | void SetLiveReg(int rx, DomainValue *DV); |
| 161 | void Kill(int rx); |
| 162 | void Force(int rx, unsigned domain); |
| 163 | void Collapse(DomainValue *dv, unsigned domain); |
| 164 | bool Merge(DomainValue *A, DomainValue *B); |
| 165 | |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 166 | void enterBasicBlock(); |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 167 | void visitGenericInstr(MachineInstr*); |
| 168 | void visitSoftInstr(MachineInstr*, unsigned mask); |
| 169 | void visitHardInstr(MachineInstr*, unsigned domain); |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 170 | }; |
| 171 | } |
| 172 | |
| 173 | char SSEDomainFixPass::ID = 0; |
| 174 | |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 175 | /// Translate TRI register number to an index into our smaller tables of |
| 176 | /// interesting registers. Return -1 for boring registers. |
| 177 | int SSEDomainFixPass::RegIndex(unsigned reg) { |
| 178 | // Registers are sorted lexicographically. |
| 179 | // We just need them to be consecutive, ordering doesn't matter. |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 180 | assert(X86::XMM9 == X86::XMM0+NumRegs-1 && "Unexpected sort"); |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 181 | reg -= X86::XMM0; |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 182 | return reg < NumRegs ? reg : -1; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /// Set LiveRegs[rx] = dv, updating reference counts. |
| 186 | void SSEDomainFixPass::SetLiveReg(int rx, DomainValue *dv) { |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 187 | assert(unsigned(rx) < NumRegs && "Invalid index"); |
| 188 | if (!LiveRegs) |
| 189 | LiveRegs = (DomainValue**)calloc(sizeof(DomainValue*), NumRegs); |
| 190 | |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 191 | if (LiveRegs[rx] == dv) |
| 192 | return; |
| 193 | if (LiveRegs[rx]) { |
| 194 | assert(LiveRegs[rx]->Refs && "Bad refcount"); |
| 195 | if (--LiveRegs[rx]->Refs == 0) Pool.Recycle(LiveRegs[rx]); |
| 196 | } |
| 197 | LiveRegs[rx] = dv; |
| 198 | if (dv) ++dv->Refs; |
| 199 | } |
| 200 | |
| 201 | // Kill register rx, recycle or collapse any DomainValue. |
| 202 | void SSEDomainFixPass::Kill(int rx) { |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 203 | assert(unsigned(rx) < NumRegs && "Invalid index"); |
| 204 | if (!LiveRegs || !LiveRegs[rx]) return; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 205 | |
| 206 | // Before killing the last reference to an open DomainValue, collapse it to |
| 207 | // the first available domain. |
| 208 | if (LiveRegs[rx]->Refs == 1 && !LiveRegs[rx]->collapsed()) |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 209 | Collapse(LiveRegs[rx], LiveRegs[rx]->firstDomain()); |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 210 | else |
| 211 | SetLiveReg(rx, 0); |
| 212 | } |
| 213 | |
| 214 | /// Force register rx into domain. |
| 215 | void SSEDomainFixPass::Force(int rx, unsigned domain) { |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 216 | assert(unsigned(rx) < NumRegs && "Invalid index"); |
Jakob Stoklund Olesen | d77f818 | 2010-03-30 00:09:32 +0000 | [diff] [blame] | 217 | DomainValue *dv; |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 218 | if (LiveRegs && (dv = LiveRegs[rx])) { |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 219 | if (dv->collapsed()) |
| 220 | dv->add(domain); |
| 221 | else |
| 222 | Collapse(dv, domain); |
| 223 | } else { |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 224 | // Set up basic collapsed DomainValue. |
Jakob Stoklund Olesen | d77f818 | 2010-03-30 00:09:32 +0000 | [diff] [blame] | 225 | dv = Pool.Alloc(); |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 226 | dv->Dist = Distance; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 227 | dv->add(domain); |
| 228 | SetLiveReg(rx, dv); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /// Collapse open DomainValue into given domain. If there are multiple |
| 233 | /// registers using dv, they each get a unique collapsed DomainValue. |
| 234 | void SSEDomainFixPass::Collapse(DomainValue *dv, unsigned domain) { |
| 235 | assert(dv->compat(1u << domain) && "Cannot collapse"); |
| 236 | |
| 237 | // Collapse all the instructions. |
| 238 | while (!dv->Instrs.empty()) { |
| 239 | MachineInstr *mi = dv->Instrs.back(); |
| 240 | TII->SetSSEDomain(mi, domain); |
| 241 | dv->Instrs.pop_back(); |
| 242 | } |
| 243 | dv->Mask = 1u << domain; |
| 244 | |
| 245 | // If there are multiple users, give them new, unique DomainValues. |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 246 | if (LiveRegs && dv->Refs > 1) { |
| 247 | for (unsigned rx = 0; rx != NumRegs; ++rx) |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 248 | if (LiveRegs[rx] == dv) { |
| 249 | DomainValue *dv2 = Pool.Alloc(); |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 250 | dv2->Dist = Distance; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 251 | dv2->add(domain); |
| 252 | SetLiveReg(rx, dv2); |
| 253 | } |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | /// Merge - All instructions and registers in B are moved to A, and B is |
| 258 | /// released. |
| 259 | bool SSEDomainFixPass::Merge(DomainValue *A, DomainValue *B) { |
| 260 | assert(!A->collapsed() && "Cannot merge into collapsed"); |
| 261 | assert(!B->collapsed() && "Cannot merge from collapsed"); |
Jakob Stoklund Olesen | 85ffee2 | 2010-03-31 20:05:12 +0000 | [diff] [blame] | 262 | if (A == B) |
Jakob Stoklund Olesen | 5f282b5 | 2010-03-31 17:13:16 +0000 | [diff] [blame] | 263 | return true; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 264 | if (!A->compat(B->Mask)) |
| 265 | return false; |
| 266 | A->Mask &= B->Mask; |
| 267 | A->Dist = std::max(A->Dist, B->Dist); |
| 268 | A->Instrs.append(B->Instrs.begin(), B->Instrs.end()); |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 269 | for (unsigned rx = 0; rx != NumRegs; ++rx) |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 270 | if (LiveRegs[rx] == B) |
| 271 | SetLiveReg(rx, A); |
| 272 | return true; |
| 273 | } |
| 274 | |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 275 | void SSEDomainFixPass::enterBasicBlock() { |
| 276 | // Try to coalesce live-out registers from predecessors. |
| 277 | for (MachineBasicBlock::const_livein_iterator i = MBB->livein_begin(), |
| 278 | e = MBB->livein_end(); i != e; ++i) { |
| 279 | int rx = RegIndex(*i); |
| 280 | if (rx < 0) continue; |
| 281 | for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(), |
| 282 | pe = MBB->pred_end(); pi != pe; ++pi) { |
Jakob Stoklund Olesen | b16df90 | 2010-03-31 00:40:08 +0000 | [diff] [blame] | 283 | LiveOutMap::const_iterator fi = LiveOuts.find(*pi); |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 284 | if (fi == LiveOuts.end()) continue; |
| 285 | DomainValue *pdv = fi->second[rx]; |
| 286 | if (!pdv) continue; |
Chris Lattner | 563d83f | 2010-03-31 20:32:51 +0000 | [diff] [blame^] | 287 | if (!LiveRegs || !LiveRegs[rx]) { |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 288 | SetLiveReg(rx, pdv); |
Chris Lattner | 563d83f | 2010-03-31 20:32:51 +0000 | [diff] [blame^] | 289 | continue; |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 290 | } |
Chris Lattner | 563d83f | 2010-03-31 20:32:51 +0000 | [diff] [blame^] | 291 | |
| 292 | // We have a live DomainValue from more than one predecessor. |
| 293 | if (LiveRegs[rx]->collapsed()) { |
| 294 | // We are already collapsed, but predecessor is not. Force him. |
| 295 | if (!pdv->collapsed()) |
| 296 | Collapse(pdv, LiveRegs[rx]->firstDomain()); |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | // Currently open, merge in predecessor. |
| 301 | if (!pdv->collapsed()) |
| 302 | Merge(LiveRegs[rx], pdv); |
| 303 | else |
| 304 | Collapse(LiveRegs[rx], pdv->firstDomain()); |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // A hard instruction only works in one domain. All input registers will be |
| 310 | // forced into that domain. |
| 311 | void SSEDomainFixPass::visitHardInstr(MachineInstr *mi, unsigned domain) { |
| 312 | // Collapse all uses. |
| 313 | for (unsigned i = mi->getDesc().getNumDefs(), |
| 314 | e = mi->getDesc().getNumOperands(); i != e; ++i) { |
| 315 | MachineOperand &mo = mi->getOperand(i); |
| 316 | if (!mo.isReg()) continue; |
| 317 | int rx = RegIndex(mo.getReg()); |
| 318 | if (rx < 0) continue; |
| 319 | Force(rx, domain); |
| 320 | } |
| 321 | |
| 322 | // Kill all defs and force them. |
| 323 | for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) { |
| 324 | MachineOperand &mo = mi->getOperand(i); |
| 325 | if (!mo.isReg()) continue; |
| 326 | int rx = RegIndex(mo.getReg()); |
| 327 | if (rx < 0) continue; |
| 328 | Kill(rx); |
| 329 | Force(rx, domain); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // A soft instruction can be changed to work in other domains given by mask. |
| 334 | void SSEDomainFixPass::visitSoftInstr(MachineInstr *mi, unsigned mask) { |
| 335 | // Scan the explicit use operands for incoming domains. |
| 336 | unsigned collmask = mask; |
| 337 | SmallVector<int, 4> used; |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 338 | if (LiveRegs) |
| 339 | for (unsigned i = mi->getDesc().getNumDefs(), |
| 340 | e = mi->getDesc().getNumOperands(); i != e; ++i) { |
Chris Lattner | 563d83f | 2010-03-31 20:32:51 +0000 | [diff] [blame^] | 341 | MachineOperand &mo = mi->getOperand(i); |
| 342 | if (!mo.isReg()) continue; |
| 343 | int rx = RegIndex(mo.getReg()); |
| 344 | if (rx < 0) continue; |
| 345 | if (DomainValue *dv = LiveRegs[rx]) { |
| 346 | // Is it possible to use this collapsed register for free? |
| 347 | if (dv->collapsed()) { |
| 348 | if (unsigned m = collmask & dv->Mask) |
| 349 | collmask = m; |
| 350 | } else if (dv->compat(collmask)) |
| 351 | used.push_back(rx); |
| 352 | else |
| 353 | Kill(rx); |
| 354 | } |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 355 | } |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 356 | |
| 357 | // If the collapsed operands force a single domain, propagate the collapse. |
| 358 | if (isPowerOf2_32(collmask)) { |
| 359 | unsigned domain = CountTrailingZeros_32(collmask); |
| 360 | TII->SetSSEDomain(mi, domain); |
| 361 | visitHardInstr(mi, domain); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | // Kill off any remaining uses that don't match collmask, and build a list of |
| 366 | // incoming DomainValue that we want to merge. |
| 367 | SmallVector<DomainValue*,4> doms; |
| 368 | for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) { |
| 369 | int rx = *i; |
| 370 | DomainValue *dv = LiveRegs[rx]; |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 371 | // This useless DomainValue could have been missed above. |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 372 | if (!dv->compat(collmask)) { |
| 373 | Kill(*i); |
| 374 | continue; |
| 375 | } |
| 376 | // sorted, uniqued insert. |
| 377 | bool inserted = false; |
| 378 | for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end(); |
| 379 | i != e && !inserted; ++i) { |
| 380 | if (dv == *i) |
| 381 | inserted = true; |
| 382 | else if (dv->Dist < (*i)->Dist) { |
| 383 | inserted = true; |
| 384 | doms.insert(i, dv); |
| 385 | } |
| 386 | } |
| 387 | if (!inserted) |
| 388 | doms.push_back(dv); |
| 389 | } |
| 390 | |
| 391 | // doms are now sorted in order of appearance. Try to merge them all, giving |
| 392 | // priority to the latest ones. |
| 393 | DomainValue *dv = 0; |
| 394 | while (!doms.empty()) { |
Chris Lattner | 563d83f | 2010-03-31 20:32:51 +0000 | [diff] [blame^] | 395 | if (!dv) { |
| 396 | dv = doms.pop_back_val(); |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | DomainValue *ThisDV = doms.pop_back_val(); |
| 401 | if (Merge(dv, ThisDV)) continue; |
| 402 | |
| 403 | for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i) |
| 404 | if (LiveRegs[*i] == ThisDV) |
| 405 | Kill(*i); |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // dv is the DomainValue we are going to use for this instruction. |
| 409 | if (!dv) |
| 410 | dv = Pool.Alloc(); |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 411 | dv->Dist = Distance; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 412 | dv->Mask = collmask; |
| 413 | dv->Instrs.push_back(mi); |
| 414 | |
| 415 | // Finally set all defs and non-collapsed uses to dv. |
| 416 | for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) { |
| 417 | MachineOperand &mo = mi->getOperand(i); |
| 418 | if (!mo.isReg()) continue; |
| 419 | int rx = RegIndex(mo.getReg()); |
| 420 | if (rx < 0) continue; |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 421 | if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) { |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 422 | Kill(rx); |
| 423 | SetLiveReg(rx, dv); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | void SSEDomainFixPass::visitGenericInstr(MachineInstr *mi) { |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 429 | // Process explicit defs, kill any XMM registers redefined. |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 430 | for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) { |
| 431 | MachineOperand &mo = mi->getOperand(i); |
| 432 | if (!mo.isReg()) continue; |
| 433 | int rx = RegIndex(mo.getReg()); |
| 434 | if (rx < 0) continue; |
| 435 | Kill(rx); |
| 436 | } |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | bool SSEDomainFixPass::runOnMachineFunction(MachineFunction &mf) { |
| 440 | MF = &mf; |
| 441 | TII = static_cast<const X86InstrInfo*>(MF->getTarget().getInstrInfo()); |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 442 | TRI = MF->getTarget().getRegisterInfo(); |
| 443 | MBB = 0; |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 444 | LiveRegs = 0; |
| 445 | Distance = 0; |
| 446 | assert(NumRegs == X86::VR128RegClass.getNumRegs() && "Bad regclass"); |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 447 | |
| 448 | // If no XMM registers are used in the function, we can skip it completely. |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 449 | bool anyregs = false; |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 450 | for (TargetRegisterClass::const_iterator I = X86::VR128RegClass.begin(), |
| 451 | E = X86::VR128RegClass.end(); I != E; ++I) |
| 452 | if (MF->getRegInfo().isPhysRegUsed(*I)) { |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 453 | anyregs = true; |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 454 | break; |
| 455 | } |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 456 | if (!anyregs) return false; |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 457 | |
| 458 | MachineBasicBlock *Entry = MF->begin(); |
| 459 | SmallPtrSet<MachineBasicBlock*, 16> Visited; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 460 | for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 16> > |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 461 | DFI = df_ext_begin(Entry, Visited), DFE = df_ext_end(Entry, Visited); |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 462 | DFI != DFE; ++DFI) { |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 463 | MBB = *DFI; |
| 464 | enterBasicBlock(); |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 465 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; |
| 466 | ++I) { |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 467 | MachineInstr *mi = I; |
| 468 | if (mi->isDebugValue()) continue; |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 469 | ++Distance; |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 470 | std::pair<uint16_t, uint16_t> domp = TII->GetSSEDomain(mi); |
| 471 | if (domp.first) |
| 472 | if (domp.second) |
| 473 | visitSoftInstr(mi, domp.second); |
| 474 | else |
| 475 | visitHardInstr(mi, domp.first); |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 476 | else if (LiveRegs) |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 477 | visitGenericInstr(mi); |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 478 | } |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 479 | |
| 480 | // Save live registers at end of MBB - used by enterBasicBlock(). |
| 481 | if (LiveRegs) |
| 482 | LiveOuts.insert(std::make_pair(MBB, LiveRegs)); |
| 483 | LiveRegs = 0; |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 484 | } |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 485 | |
Jakob Stoklund Olesen | 1a5d2a8 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 486 | // Clear the LiveOuts vectors. Should we also collapse any remaining |
| 487 | // DomainValues? |
| 488 | for (LiveOutMap::const_iterator i = LiveOuts.begin(), e = LiveOuts.end(); |
| 489 | i != e; ++i) |
| 490 | free(i->second); |
| 491 | LiveOuts.clear(); |
Jakob Stoklund Olesen | e4b94b4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 492 | Pool.Clear(); |
| 493 | |
Jakob Stoklund Olesen | 352aa50 | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | |
| 497 | FunctionPass *llvm::createSSEDomainFixPass() { |
| 498 | return new SSEDomainFixPass(); |
| 499 | } |