llvm.org GIT mirror llvm / 4efe741
Properly handle kills of a physical register which has sub-registers that are read by later instructions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37739 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 13 years ago
2 changed file(s) with 85 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
3030
3131 #include "llvm/CodeGen/MachineFunctionPass.h"
3232 #include "llvm/ADT/BitVector.h"
33 #include "llvm/ADT/SmallSet.h"
3334 #include "llvm/ADT/SmallVector.h"
3435 #include
3536
162163 bool addRegisterDead(unsigned IncomingReg, MachineInstr *MI,
163164 bool AddIfNotFound = false);
164165
166 void addRegisterKills(unsigned Reg, MachineInstr *MI,
167 SmallSet &SubKills);
168
169 /// HandlePhysRegKill - Add kills of Reg and its sub-registers to the
170 /// uses. Pay special attention to the sub-register uses which may come below
171 /// the last use of the whole register.
172 bool HandlePhysRegKill(unsigned Reg, MachineInstr *MI,
173 SmallSet &SubKills);
174 bool HandlePhysRegKill(unsigned Reg, MachineInstr *MI);
165175 void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
166176 void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);
167177
285285 PhysRegPartUse[SuperReg] = MI;
286286 }
287287
288 bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI,
289 SmallSet &SubKills) {
290 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
291 unsigned SubReg = *SubRegs; ++SubRegs) {
292 MachineInstr *LastRef = PhysRegInfo[SubReg];
293 if (LastRef != RefMI)
294 SubKills.insert(SubReg);
295 else if (!HandlePhysRegKill(SubReg, RefMI, SubKills))
296 SubKills.insert(SubReg);
297 }
298
299 if (*RegInfo->getImmediateSubRegisters(Reg) == 0) {
300 // No sub-registers, just check if reg is killed by RefMI.
301 if (PhysRegInfo[Reg] == RefMI)
302 return true;
303 } else if (SubKills.empty())
304 // None of the sub-registers are killed elsewhere...
305 return true;
306 return false;
307 }
308
309 void LiveVariables::addRegisterKills(unsigned Reg, MachineInstr *MI,
310 SmallSet &SubKills) {
311 if (SubKills.count(Reg) == 0)
312 addRegisterKilled(Reg, MI, true);
313 else {
314 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
315 unsigned SubReg = *SubRegs; ++SubRegs)
316 addRegisterKills(SubReg, MI, SubKills);
317 }
318 }
319
320 bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *RefMI) {
321 SmallSet SubKills;
322 if (HandlePhysRegKill(Reg, RefMI, SubKills)) {
323 addRegisterKilled(Reg, RefMI);
324 return true;
325 } else {
326 // Some sub-registers are killed by another MI.
327 for (const unsigned *SubRegs = RegInfo->getImmediateSubRegisters(Reg);
328 unsigned SubReg = *SubRegs; ++SubRegs)
329 addRegisterKills(SubReg, RefMI, SubKills);
330 return false;
331 }
332 }
333
288334 void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
289335 // Does this kill a previous version of this register?
290336 if (MachineInstr *LastRef = PhysRegInfo[Reg]) {
291 if (PhysRegUsed[Reg])
292 addRegisterKilled(Reg, LastRef);
293 else if (PhysRegPartUse[Reg])
337 if (PhysRegUsed[Reg]) {
338 if (!HandlePhysRegKill(Reg, LastRef)) {
339 if (PhysRegPartUse[Reg])
340 addRegisterKilled(Reg, PhysRegPartUse[Reg], true);
341 }
342 } else if (PhysRegPartUse[Reg])
294343 // Add implicit use / kill to last use of a sub-register.
295344 addRegisterKilled(Reg, PhysRegPartUse[Reg], true);
296345 else
297346 addRegisterDead(Reg, LastRef);
298347 }
299 PhysRegInfo[Reg] = MI;
300 PhysRegUsed[Reg] = false;
301 PhysRegPartUse[Reg] = NULL;
302348
303349 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
304350 unsigned SubReg = *SubRegs; ++SubRegs) {
305351 if (MachineInstr *LastRef = PhysRegInfo[SubReg]) {
306 if (PhysRegUsed[SubReg])
307 addRegisterKilled(SubReg, LastRef);
308 else if (PhysRegPartUse[SubReg])
352 if (PhysRegUsed[SubReg]) {
353 if (!HandlePhysRegKill(SubReg, LastRef)) {
354 if (PhysRegPartUse[SubReg])
355 addRegisterKilled(SubReg, PhysRegPartUse[SubReg], true);
356 }
357 //addRegisterKilled(SubReg, LastRef);
358 } else if (PhysRegPartUse[SubReg])
309359 // Add implicit use / kill to last use of a sub-register.
310360 addRegisterKilled(SubReg, PhysRegPartUse[SubReg], true);
311361 else
312362 addRegisterDead(SubReg, LastRef);
313363 }
314 PhysRegInfo[SubReg] = MI;
315 PhysRegUsed[SubReg] = false;
316 PhysRegPartUse[SubReg] = NULL;
317 }
318
319 if (MI)
364 }
365
366 if (MI) {
320367 for (const unsigned *SuperRegs = RegInfo->getSuperRegisters(Reg);
321368 unsigned SuperReg = *SuperRegs; ++SuperRegs) {
322369 if (PhysRegInfo[SuperReg]) {
333380 // Remember this partial def.
334381 PhysRegPartDef[SuperReg].push_back(MI);
335382 }
383 }
384
385 PhysRegInfo[Reg] = MI;
386 PhysRegUsed[Reg] = false;
387 PhysRegPartUse[Reg] = NULL;
388 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
389 unsigned SubReg = *SubRegs; ++SubRegs) {
390 PhysRegInfo[SubReg] = MI;
391 PhysRegUsed[SubReg] = false;
392 PhysRegPartUse[SubReg] = NULL;
393 }
336394 }
337395 }
338396
464522 // Clear some states between BB's. These are purely local information.
465523 for (unsigned i = 0; i != NumRegs; ++i)
466524 PhysRegPartDef[i].clear();
525 std::fill(PhysRegInfo, PhysRegInfo + NumRegs, (MachineInstr*)0);
526 std::fill(PhysRegUsed, PhysRegUsed + NumRegs, false);
467527 std::fill(PhysRegPartUse, PhysRegPartUse + NumRegs, (MachineInstr*)0);
468528 }
469529