llvm.org GIT mirror llvm / bb96dfc
MachineCopyPropagation has special logic for removing COPY instructions. It will remove plain COPYs using eraseFromParent(), but if the COPY has imp-defs/imp-uses it will convert it to a KILL, to keep the imp-def around. This actually totally breaks and causes the machine verifier to cry in several cases, one of which being: %RAX<def> = COPY %RCX<kill> %ECX<def> = COPY %EAX<kill>, %RAX<imp-use,kill> These subregister copies are together identified as noops, so are both removed. However, the second one as it has an imp-use gets converted into a kill: %ECX<def> = KILL %EAX<kill>, %RAX<imp-use,kill> As the original COPY has been removed, the verifier goes into tears at the use of undefined EAX and RAX. There are several hacky solutions to this hacky problem (which is all to do with imp-use/def weirdnesses), but the least hacky I've come up with is to *always* remove COPYs by converting to KILLs. KILLs are no-ops to the code generator so the generated code doesn't change (which is why they were partially used in the first place), but using them also keeps the def/use and imp-def/imp-use chains alive: %RAX<def> = KILL %RCX<kill> %ECX<def> = KILL %EAX<kill>, %RAX<imp-use,kill> The patch passes all test cases including the ones that check the removal of MOVs in this circumstance, along with an extra test I added to check subregister behaviour (which made the machine verifier fall over before my patch). The patch also adds some DEBUG() statements because the file hadn't got any. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199797 91177308-0d34-0410-b5e6-96231b3b80d8 James Molloy 6 years ago
2 changed file(s) with 43 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
126126 }
127127
128128 // Remove MI from the function because it has been determined it is dead.
129 // Turn it into a noop KILL instruction if it has super-register liveness
130 // adjustments.
129 // Turn it into a noop KILL instruction as opposed to removing it to
130 // maintain imp-use/imp-def chains.
131131 void MachineCopyPropagation::removeCopy(MachineInstr *MI) {
132 if (MI->getNumOperands() == 2)
133 MI->eraseFromParent();
134 else
135 MI->setDesc(TII->get(TargetOpcode::KILL));
132 MI->setDesc(TII->get(TargetOpcode::KILL));
136133 }
137134
138135 bool MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
140137 DenseMap AvailCopyMap; // Def -> available copies map
141138 DenseMap CopyMap; // Def -> copies map
142139 SourceMap SrcMap; // Src -> Def map
140
141 DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
143142
144143 bool Changed = false;
145144 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
175174 // CALL
176175 // %RAX = COPY %RSP
177176
177 DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; MI->dump());
178
178179 // Clear any kills of Def between CopyMI and MI. This extends the
179180 // live range.
180181 for (MachineBasicBlock::iterator I = CopyMI, E = MI; I != E; ++I)
190191 // If Src is defined by a previous copy, it cannot be eliminated.
191192 for (MCRegAliasIterator AI(Src, TRI, true); AI.isValid(); ++AI) {
192193 CI = CopyMap.find(*AI);
193 if (CI != CopyMap.end())
194 if (CI != CopyMap.end()) {
195 DEBUG(dbgs() << "MCP: Copy is no longer dead: "; CI->second->dump());
194196 MaybeDeadCopies.remove(CI->second);
195 }
197 }
198 }
199
200 DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
196201
197202 // Copy is now a candidate for deletion.
198203 MaybeDeadCopies.insert(MI);
254259 // for elimination.
255260 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
256261 DenseMap::iterator CI = CopyMap.find(*AI);
257 if (CI != CopyMap.end())
262 if (CI != CopyMap.end()) {
263 DEBUG(dbgs() << "MCP: Copy is used - not dead: "; CI->second->dump());
258264 MaybeDeadCopies.remove(CI->second);
265 }
259266 }
260267 }
261268
272279 unsigned Reg = (*DI)->getOperand(0).getReg();
273280 if (MRI->isReserved(Reg) || !MaskMO.clobbersPhysReg(Reg))
274281 continue;
282 DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
283 (*DI)->dump());
275284 removeCopy(*DI);
276285 Changed = true;
277286 ++NumDeletes;
None ; RUN: llc -mtriple=x86_64-apple-macosx -mcpu=nocona < %s | FileCheck %s
0 ; RUN: llc -mtriple=x86_64-apple-macosx -mcpu=nocona -verify-machineinstrs < %s | FileCheck %s
11
22 ; After tail duplication, two copies in an early exit BB can be cancelled out.
33 ; rdar://10640363
3333 %tmp8 = shufflevector <8 x i16> %T0, <8 x i16> %T1, <8 x i32> < i32 undef, i32 undef, i32 7, i32 2, i32 8, i32 undef, i32 undef , i32 undef >
3434 ret <8 x i16> %tmp8
3535 }
36
37 define i32 @t3(i64 %a, i64 %b) nounwind {
38 entry:
39 ; CHECK-LABEL: t3:
40 ; CHECK: je [[LABEL:.*BB.*]]
41 %cmp1 = icmp eq i64 %b, 0
42 br i1 %cmp1, label %while.end, label %while.body
43
44 ; CHECK: [[LABEL]]:
45 ; CHECK-NOT: mov
46 ; CHECK: ret
47
48 while.body: ; preds = %entry, %while.body
49 %a.addr.03 = phi i64 [ %b.addr.02, %while.body ], [ %a, %entry ]
50 %b.addr.02 = phi i64 [ %rem, %while.body ], [ %b, %entry ]
51 %rem = srem i64 %a.addr.03, %b.addr.02
52 %cmp = icmp eq i64 %rem, 0
53 br i1 %cmp, label %while.end, label %while.body
54
55 while.end: ; preds = %while.body, %entry
56 %a.addr.0.lcssa = phi i64 [ %a, %entry ], [ %b.addr.02, %while.body ]
57 %t = trunc i64 %a.addr.0.lcssa to i32
58 ret i32 %t
59 }