llvm.org GIT mirror llvm / 9c2e7ca
Be more forgiving when calculating alias interference for physreg coalescing. It is OK for an alias live range to overlap if there is a copy to or from the physical register. CoalescerPair can work out if the copy is coalescable independently of the alias. This means that we can join with the actual destination interval instead of using the getOrigDstReg() hack. It is no longer necessary to merge clobber ranges into subregisters. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@107695 91177308-0d34-0410-b5e6-96231b3b80d8 Jakob Stoklund Olesen 9 years ago
5 changed file(s) with 63 addition(s) and 117 deletion(s). Raw diff Collapse all Expand all
164164 /// virtual register.
165165 unsigned subIdx_;
166166
167 /// origDstReg_ - dstReg_ without subreg adjustments.
168 unsigned origDstReg_;
169
170167 /// partial_ - True when the original copy was a partial subregister copy.
171168 bool partial_;
172169
191188 public:
192189 CoalescerPair(const TargetInstrInfo &tii, const TargetRegisterInfo &tri)
193190 : tii_(tii), tri_(tri), dstReg_(0), srcReg_(0), subIdx_(0),
194 origDstReg_(0), partial_(false), crossClass_(false), flipped_(false),
195 newRC_(0) {}
191 partial_(false), crossClass_(false), flipped_(false), newRC_(0) {}
196192
197193 /// setRegisters - set registers to match the copy instruction MI. Return
198194 /// false if MI is not a coalescable copy instruction.
230226 /// getSubIdx - Return the subregister index in DstReg that SrcReg will be
231227 /// coalesced into, or 0.
232228 unsigned getSubIdx() const { return subIdx_; }
233
234 /// getOrigDstReg - Return DstReg as it appeared in the original copy
235 /// instruction before any subreg adjustments.
236 unsigned getOrigDstReg() const { return isPhys() ? origDstReg_ : dstReg_; }
237229
238230 /// getNewRC - Return the register class of the coalesced register.
239231 const TargetRegisterClass *getNewRC() const { return newRC_; }
8282 std::swap(SrcSub, DstSub);
8383 flipped_ = true;
8484 }
85 origDstReg_ = Dst;
8685
8786 const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
8887
100100 ///
101101 bool SimpleRegisterCoalescing::AdjustCopiesBackFrom(const CoalescerPair &CP,
102102 MachineInstr *CopyMI) {
103 // Bail if there is no dst interval - can happen when merging physical subreg
104 // operations.
105 if (!li_->hasInterval(CP.getDstReg()))
106 return false;
107
103108 LiveInterval &IntA =
104109 li_->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
105110 LiveInterval &IntB =
109114 // BValNo is a value number in B that is defined by a copy from A. 'B3' in
110115 // the example above.
111116 LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
112 assert(BLR != IntB.end() && "Live range not found!");
117 if (BLR == IntB.end()) return false;
113118 VNInfo *BValNo = BLR->valno;
114119
115120 // Get the location that B is defined at. Two options: either this value has
300305 ///
301306 /// This returns true if an interval was modified.
302307 ///
303 bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(LiveInterval &IntA,
304 LiveInterval &IntB,
308 bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(const CoalescerPair &CP,
305309 MachineInstr *CopyMI) {
306 SlotIndex CopyIdx =
307 li_->getInstructionIndex(CopyMI).getDefIndex();
308
309310 // FIXME: For now, only eliminate the copy by commuting its def when the
310311 // source register is a virtual register. We want to guard against cases
311312 // where the copy is a back edge copy and commuting the def lengthen the
312313 // live interval of the source register to the entire loop.
313 if (TargetRegisterInfo::isPhysicalRegister(IntA.reg))
314 return false;
314 if (CP.isPhys() && CP.isFlipped())
315 return false;
316
317 // Bail if there is no dst interval.
318 if (!li_->hasInterval(CP.getDstReg()))
319 return false;
320
321 SlotIndex CopyIdx =
322 li_->getInstructionIndex(CopyMI).getDefIndex();
323
324 LiveInterval &IntA =
325 li_->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg());
326 LiveInterval &IntB =
327 li_->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg());
315328
316329 // BValNo is a value number in B that is defined by a copy from A. 'B3' in
317330 // the example above.
318331 LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
319 assert(BLR != IntB.end() && "Live range not found!");
332 if (BLR == IntB.end()) return false;
320333 VNInfo *BValNo = BLR->valno;
321334
322335 // Get the location that B is defined at. Two options: either this value has
504517 if (EI != BExtend.end())
505518 End = EI->second;
506519 IntB.addRange(LiveRange(AI->start, End, ValNo));
507
508 // If the IntB live range is assigned to a physical register, and if that
509 // physreg has sub-registers, update their live intervals as well.
510 if (BHasSubRegs) {
511 for (const unsigned *SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR) {
512 LiveInterval &SRLI = li_->getInterval(*SR);
513 SRLI.MergeInClobberRange(*li_, AI->start, End,
514 li_->getVNInfoAllocator());
515 }
516 }
517520 }
518521 ValNo->setHasPHIKill(BHasPHIKill);
519522
11331136 }
11341137 }
11351138
1136 // We may need the source interval after JoinIntervals has destroyed it.
1137 OwningPtr SavedLI;
1138 if (CP.getOrigDstReg() != CP.getDstReg())
1139 SavedLI.reset(li_->dupInterval(&li_->getInterval(CP.getSrcReg())));
1140
11411139 // Okay, attempt to join these two intervals. On failure, this returns false.
11421140 // Otherwise, if one of the intervals being joined is a physreg, this method
11431141 // always canonicalizes DstInt to be it. The output "SrcInt" will not have
11541152
11551153 // If we can eliminate the copy without merging the live ranges, do so now.
11561154 if (!CP.isPartial()) {
1157 LiveInterval *UseInt = &li_->getInterval(CP.getSrcReg());
1158 LiveInterval *DefInt = &li_->getInterval(CP.getDstReg());
1159 if (CP.isFlipped())
1160 std::swap(UseInt, DefInt);
11611155 if (AdjustCopiesBackFrom(CP, CopyMI) ||
1162 RemoveCopyByCommutingDef(*UseInt, *DefInt, CopyMI)) {
1156 RemoveCopyByCommutingDef(CP, CopyMI)) {
11631157 JoinedCopies.insert(CopyMI);
11641158 DEBUG(dbgs() << "\tTrivial!\n");
11651159 return true;
11701164 DEBUG(dbgs() << "\tInterference!\n");
11711165 Again = true; // May be possible to coalesce later.
11721166 return false;
1173 }
1174
1175 if (CP.isPhys()) {
1176 // If this is a extract_subreg where dst is a physical register, e.g.
1177 // cl = EXTRACT_SUBREG reg1024, 1
1178 // then create and update the actual physical register allocated to RHS.
1179 unsigned LargerDstReg = CP.getDstReg();
1180 if (CP.getOrigDstReg() != CP.getDstReg()) {
1181 if (tri_->isSubRegister(CP.getOrigDstReg(), LargerDstReg))
1182 LargerDstReg = CP.getOrigDstReg();
1183 LiveInterval &RealInt = li_->getOrCreateInterval(CP.getDstReg());
1184 for (LiveInterval::const_vni_iterator I = SavedLI->vni_begin(),
1185 E = SavedLI->vni_end(); I != E; ++I) {
1186 const VNInfo *ValNo = *I;
1187 VNInfo *NewValNo = RealInt.getNextValue(ValNo->def, ValNo->getCopy(),
1188 false, // updated at *
1189 li_->getVNInfoAllocator());
1190 NewValNo->setFlags(ValNo->getFlags()); // * updated here.
1191 RealInt.MergeValueInAsValue(*SavedLI, ValNo, NewValNo);
1192 }
1193 RealInt.weight += SavedLI->weight;
1194 }
1195
1196 // Update the liveintervals of sub-registers.
1197 LiveInterval &LargerInt = li_->getInterval(LargerDstReg);
1198 for (const unsigned *AS = tri_->getSubRegisters(LargerDstReg); *AS; ++AS) {
1199 LiveInterval &SRI = li_->getOrCreateInterval(*AS);
1200 SRI.MergeInClobberRanges(*li_, LargerInt, li_->getVNInfoAllocator());
1201 DEBUG({
1202 dbgs() << "\t\tsubreg: "; SRI.print(dbgs(), tri_); dbgs() << "\n";
1203 });
1204 }
12051167 }
12061168
12071169 // Coalescing to a virtual register that is of a sub-register class of the
13101272 LiveInterval &RHS = li_->getInterval(CP.getSrcReg());
13111273 DEBUG({ dbgs() << "\t\tRHS = "; RHS.print(dbgs(), tri_); dbgs() << "\n"; });
13121274
1313 // FIXME: Join into CP.getDstReg instead of CP.getOrigDstReg.
1314 // When looking at
1315 // %reg2000 = EXTRACT_SUBREG %EAX, sub_16bit
1316 // we really want to join %reg2000 with %AX ( = CP.getDstReg). We are actually
1317 // joining into %EAX ( = CP.getOrigDstReg) because it is guaranteed to have an
1318 // existing live interval, and we are better equipped to handle interference.
1319 // JoinCopy cleans up the mess by taking a copy of RHS before calling here,
1320 // and merging that copy into CP.getDstReg after.
1321
1322 // If a live interval is a physical register, conservatively check if any
1323 // of its sub-registers is overlapping the live interval of the virtual
1324 // register. If so, do not coalesce.
1325 if (CP.isPhys() && *tri_->getSubRegisters(CP.getOrigDstReg())) {
1326 // If it's coalescing a virtual register to a physical register, estimate
1327 // its live interval length. This is the *cost* of scanning an entire live
1328 // interval. If the cost is low, we'll do an exhaustive check instead.
1329
1330 // If this is something like this:
1331 // BB1:
1332 // v1024 = op
1333 // ...
1334 // BB2:
1335 // ...
1336 // RAX = v1024
1337 //
1338 // That is, the live interval of v1024 crosses a bb. Then we can't rely on
1339 // less conservative check. It's possible a sub-register is defined before
1340 // v1024 (or live in) and live out of BB1.
1341 if (RHS.containsOneValue() &&
1342 li_->intervalIsInOneMBB(RHS) &&
1343 li_->getApproximateInstructionCount(RHS) <= 10) {
1344 // Perform a more exhaustive check for some common cases.
1345 if (li_->conflictsWithAliasRef(RHS, CP.getOrigDstReg(), JoinedCopies))
1346 return false;
1347 } else {
1348 for (const unsigned* SR = tri_->getAliasSet(CP.getOrigDstReg()); *SR;
1349 ++SR)
1350 if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) {
1275 // If a live interval is a physical register, check for interference with any
1276 // aliases. The interference check implemented here is a bit more conservative
1277 // than the full interfeence check below. We allow overlapping live ranges
1278 // only when one is a copy of the other.
1279 if (CP.isPhys()) {
1280 for (const unsigned *AS = tri_->getAliasSet(CP.getDstReg()); *AS; ++AS){
1281 if (!li_->hasInterval(*AS))
1282 continue;
1283 const LiveInterval &LHS = li_->getInterval(*AS);
1284 LiveInterval::const_iterator LI = LHS.begin();
1285 for (LiveInterval::const_iterator RI = RHS.begin(), RE = RHS.end();
1286 RI != RE; ++RI) {
1287 LI = std::lower_bound(LI, LHS.end(), RI->start);
1288 // Does LHS have an overlapping live range starting before RI?
1289 if ((LI != LHS.begin() && LI[-1].end > RI->start) &&
1290 (RI->start != RI->valno->def ||
1291 !CP.isCoalescable(li_->getInstructionFromIndex(RI->start)))) {
13511292 DEBUG({
1352 dbgs() << "\tInterfere with sub-register ";
1353 li_->getInterval(*SR).print(dbgs(), tri_);
1354 });
1293 dbgs() << "\t\tInterference from alias: ";
1294 LHS.print(dbgs(), tri_);
1295 dbgs() << "\n\t\tOverlap at " << RI->start << " and no copy.\n";
1296 });
13551297 return false;
13561298 }
1299
1300 // Check that LHS ranges beginning in this range are copies.
1301 for (; LI != LHS.end() && LI->start < RI->end; ++LI) {
1302 if (LI->start != LI->valno->def ||
1303 !CP.isCoalescable(li_->getInstructionFromIndex(LI->start))) {
1304 DEBUG({
1305 dbgs() << "\t\tInterference from alias: ";
1306 LHS.print(dbgs(), tri_);
1307 dbgs() << "\n\t\tDef at " << LI->start << " is not a copy.\n";
1308 });
1309 return false;
1310 }
1311 }
1312 }
13571313 }
13581314 }
13591315
13651321 DenseMap RHSValsDefinedFromLHS;
13661322 SmallVector NewVNInfo;
13671323
1368 LiveInterval &LHS = li_->getInterval(CP.getOrigDstReg());
1324 LiveInterval &LHS = li_->getOrCreateInterval(CP.getDstReg());
13691325 DEBUG({ dbgs() << "\t\tLHS = "; LHS.print(dbgs(), tri_); dbgs() << "\n"; });
13701326
13711327 // Loop over the value numbers of the LHS, seeing if any are defined from
129129 /// If the source value number is defined by a commutable instruction and
130130 /// its other operand is coalesced to the copy dest register, see if we
131131 /// can transform the copy into a noop by commuting the definition.
132 bool RemoveCopyByCommutingDef(LiveInterval &IntA, LiveInterval &IntB,
133 MachineInstr *CopyMI);
132 bool RemoveCopyByCommutingDef(const CoalescerPair &CP,MachineInstr *CopyMI);
134133
135134 /// TrimLiveIntervalToLastUse - If there is a last use in the same basic
136135 /// block as the copy instruction, trim the ive interval to the last use
7777 ; LINUX: call .L3$pb
7878 ; LINUX-NEXT: .L3$pb:
7979 ; LINUX: popl
80 ; LINUX: addl $_GLOBAL_OFFSET_TABLE_+(.L{{.*}}-.L3$pb),
81 ; LINUX: movl pfoo@GOT(%esi),
80 ; LINUX: addl $_GLOBAL_OFFSET_TABLE_+(.L{{.*}}-.L3$pb), %[[REG3:e..]]
81 ; LINUX: movl pfoo@GOT(%[[REG3]]),
8282 ; LINUX: call afoo@PLT
8383 ; LINUX: call *
8484 }