[PATCH] D11577: Reassociate: Implement isUnmovableInstruction() without a hardcoded list

Matthias Braun matze at braunis.de
Tue Jul 28 15:00:13 PDT 2015


MatzeB created this revision.
MatzeB added reviewers: majnemer, mcrosier, baldrick.
MatzeB added a subscriber: llvm-commits.
MatzeB set the repository for this revision to rL LLVM.

isUnmovableInstruction() had a list of instructions hardcoded which are
considered unmovable. The list lacked (at least) an entry for the va_arg
and cmpxchg instructions.
Fix this by using Instruction::mayReadOrWrite() /
Instruction::mayHaveSideEffects() instead of maintaining another
instruction list.

Repository:
  rL LLVM

http://reviews.llvm.org/D11577

Files:
  lib/Transforms/Scalar/Reassociate.cpp
  test/Transforms/Reassociate/vaarg_movable.ll

Index: test/Transforms/Reassociate/vaarg_movable.ll
===================================================================
--- /dev/null
+++ test/Transforms/Reassociate/vaarg_movable.ll
@@ -0,0 +1,28 @@
+; RUN: opt -S -reassociate -die < %s | FileCheck %s
+
+; The two va_arg instructions depend on the memory/context, are therfore not
+; identical and the sub should not be optimized to 0 by reassociate.
+;
+; CHECK-LABEL @func(
+; ...
+; CHECK: %v0 = va_arg i8** %varargs, i32
+; CHECK: %v1 = va_arg i8** %varargs, i32
+; CHECK: %v0.neg = sub i32 0, %v0
+; CHECK: %sub = add i32 %v0.neg, 1
+; CHECK: %add = add i32 %sub, %v1
+; ...
+; CHECK: ret i32 %add
+define i32 @func(i32 %dummy, ...) {
+  %varargs = alloca i8*, align 8
+  %varargs1 = bitcast i8** %varargs to i8*
+  call void @llvm.va_start(i8* %varargs1)
+  %v0 = va_arg i8** %varargs, i32
+  %v1 = va_arg i8** %varargs, i32
+  %sub = sub nsw i32 %v1, %v0
+  %add = add nsw i32 %sub, 1
+  call void @llvm.va_end(i8* %varargs1)
+  ret i32 %add
+}
+
+declare void @llvm.va_start(i8*)
+declare void @llvm.va_end(i8*)
Index: lib/Transforms/Scalar/Reassociate.cpp
===================================================================
--- lib/Transforms/Scalar/Reassociate.cpp
+++ lib/Transforms/Scalar/Reassociate.cpp
@@ -255,25 +255,9 @@
   return nullptr;
 }
 
-static bool isUnmovableInstruction(Instruction *I) {
-  switch (I->getOpcode()) {
-  case Instruction::PHI:
-  case Instruction::LandingPad:
-  case Instruction::Alloca:
-  case Instruction::Load:
-  case Instruction::Invoke:
-  case Instruction::UDiv:
-  case Instruction::SDiv:
-  case Instruction::FDiv:
-  case Instruction::URem:
-  case Instruction::SRem:
-  case Instruction::FRem:
-    return true;
-  case Instruction::Call:
-    return !isa<DbgInfoIntrinsic>(I);
-  default:
-    return false;
-  }
+static bool isUnmovableInstruction(const Instruction *I) {
+  return I->mayReadOrWriteMemory() || I->mayHaveSideEffects() ||
+         I->getOpcode() == Instruction::PHI;
 }
 
 void Reassociate::BuildRankMap(Function &F) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11577.30860.patch
Type: text/x-patch
Size: 2047 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150728/ab49619a/attachment.bin>


More information about the llvm-commits mailing list