AArch64: Don't call getIterator() on iterators
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Thu, 18 Aug 2016 17:58:09 +0000 (17:58 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Thu, 18 Aug 2016 17:58:09 +0000 (17:58 +0000)
Remove an unnecessary round-trip:

    iterator => operator->() => getIterator()

In some cases, the iterator is end(), so the dereference of operator->
is invalid (UB).

The testcase only crashes with r278974 (currently reverted to
investigate this), which adds an assertion for invalid dereferences of
ilist nodes.

Fixes PR29035.

llvm-svn: 279104

llvm/lib/Target/AArch64/AArch64RedundantCopyElimination.cpp
llvm/test/CodeGen/AArch64/redundant-copy-elim-empty-mbb.ll [new file with mode: 0644]

index 26a1f5d..9e916ef 100644 (file)
@@ -156,8 +156,7 @@ bool AArch64RedundantCopyElimination::optimizeCopy(MachineBasicBlock *MBB) {
     MBB->addLiveIn(TargetReg);
 
   // Clear any kills of TargetReg between CompBr and the last removed COPY.
-  for (MachineInstr &MMI :
-       make_range(MBB->begin()->getIterator(), LastChange->getIterator()))
+  for (MachineInstr &MMI : make_range(MBB->begin(), LastChange))
     MMI.clearRegisterKills(SmallestDef, TRI);
 
   return true;
diff --git a/llvm/test/CodeGen/AArch64/redundant-copy-elim-empty-mbb.ll b/llvm/test/CodeGen/AArch64/redundant-copy-elim-empty-mbb.ll
new file mode 100644 (file)
index 0000000..27a33a2
--- /dev/null
@@ -0,0 +1,29 @@
+; RUN: llc < %s | FileCheck %s
+; Make sure we don't crash in AArch64RedundantCopyElimination when a
+; MachineBasicBlock is empty.  PR29035.
+
+target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
+target triple = "aarch64-unknown-linux-gnu"
+
+declare i8* @bar()
+
+; CHECK-LABEL: foo:
+; CHECK: tbz
+; CHECK: orr
+; CHECK: ret
+; CHECK: bl bar
+; CHECK: cbnz
+; CHECK: ret
+define i1 @foo(i1 %start) {
+entry:
+  br i1 %start, label %cleanup, label %if.end
+
+if.end:                                           ; preds = %if.end, %entry
+  %call = tail call i8* @bar()
+  %cmp = icmp eq i8* %call, null
+  br i1 %cmp, label %cleanup, label %if.end
+
+cleanup:                                          ; preds = %if.end, %entry
+  %retval.0 = phi i1 [ true, %entry ], [ false, %if.end ]
+  ret i1 %retval.0
+}