ScopDetect: Bail out for non-simple memory accesses

Volatile or atomic memory accesses are currently not supported. Neither did
we think about any special handling needed nor do we support the unknown
instructions the alias set tracker turns them into sometimes. Before this
patch, us not supporting unkown instructions in an alias set caused the
following assertion failures:

Assertion `AG.size() > 1 && "Alias groups should contain at least two accesses"'
failed

llvm-svn: 251234
diff --git a/polly/include/polly/ScopDetectionDiagnostic.h b/polly/include/polly/ScopDetectionDiagnostic.h
index be40d9e..ab51773 100644
--- a/polly/include/polly/ScopDetectionDiagnostic.h
+++ b/polly/include/polly/ScopDetectionDiagnostic.h
@@ -83,6 +83,7 @@
   rrkLoopBound,
 
   rrkFuncCall,
+  rrkNonSimpleMemoryAccess,
 
   rrkAlias,
 
@@ -762,6 +763,30 @@
   //@}
 };
 
+//===----------------------------------------------------------------------===//
+/// @brief Captures errors with non-simple memory accesses.
+class ReportNonSimpleMemoryAccess : public ReportOther {
+  //===--------------------------------------------------------------------===//
+
+  // The offending call instruction.
+  Instruction *Inst;
+
+public:
+  ReportNonSimpleMemoryAccess(Instruction *Inst);
+
+  /// @name LLVM-RTTI interface
+  //@{
+  static bool classof(const RejectReason *RR);
+  //@}
+
+  /// @name RejectReason interface
+  //@{
+  virtual std::string getMessage() const override;
+  virtual const DebugLoc &getDebugLoc() const override;
+  virtual std::string getEndUserMessage() const override;
+  //@}
+};
+
 } // namespace polly
 
 #endif // POLLY_SCOP_DETECTION_DIAGNOSTIC_H
diff --git a/polly/lib/Analysis/ScopDetection.cpp b/polly/lib/Analysis/ScopDetection.cpp
index 79461c8..2fa45ad 100644
--- a/polly/lib/Analysis/ScopDetection.cpp
+++ b/polly/lib/Analysis/ScopDetection.cpp
@@ -749,6 +749,15 @@
   if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst)) {
     Context.hasStores |= isa<StoreInst>(Inst);
     Context.hasLoads |= isa<LoadInst>(Inst);
+    if (auto *Load = dyn_cast<LoadInst>(&Inst))
+      if (!Load->isSimple())
+        return invalid<ReportNonSimpleMemoryAccess>(Context, /*Assert=*/true,
+                                                    &Inst);
+    if (auto *Store = dyn_cast<StoreInst>(&Inst))
+      if (!Store->isSimple())
+        return invalid<ReportNonSimpleMemoryAccess>(Context, /*Assert=*/true,
+                                                    &Inst);
+
     return isValidMemoryAccess(Inst, Context);
   }
 
diff --git a/polly/lib/Analysis/ScopDetectionDiagnostic.cpp b/polly/lib/Analysis/ScopDetectionDiagnostic.cpp
index 1943dfc..4eabfc9 100644
--- a/polly/lib/Analysis/ScopDetectionDiagnostic.cpp
+++ b/polly/lib/Analysis/ScopDetectionDiagnostic.cpp
@@ -349,6 +349,29 @@
 }
 
 //===----------------------------------------------------------------------===//
+// ReportNonSimpleMemoryAccess
+
+ReportNonSimpleMemoryAccess::ReportNonSimpleMemoryAccess(Instruction *Inst)
+    : ReportOther(rrkNonSimpleMemoryAccess), Inst(Inst) {}
+
+std::string ReportNonSimpleMemoryAccess::getMessage() const {
+  return "Non-simple memory access: " + *Inst;
+}
+
+const DebugLoc &ReportNonSimpleMemoryAccess::getDebugLoc() const {
+  return Inst->getDebugLoc();
+}
+
+std::string ReportNonSimpleMemoryAccess::getEndUserMessage() const {
+  return "Volatile memory accesses or memory accesses for atomic types "
+         "are not supported.";
+}
+
+bool ReportNonSimpleMemoryAccess::classof(const RejectReason *RR) {
+  return RR->getKind() == rrkNonSimpleMemoryAccess;
+}
+
+//===----------------------------------------------------------------------===//
 // ReportAlias.
 
 ReportAlias::ReportAlias(Instruction *Inst, AliasSet &AS)
diff --git a/polly/test/ScopDetect/non-simple-memory-accesses.ll b/polly/test/ScopDetect/non-simple-memory-accesses.ll
new file mode 100644
index 0000000..73b4e76
--- /dev/null
+++ b/polly/test/ScopDetect/non-simple-memory-accesses.ll
@@ -0,0 +1,30 @@
+; RUN: opt %loadPolly -polly-detect \
+; RUN:   -analyze < %s | FileCheck %s
+;
+; Verify that we do not model atomic memory accesses. We did not reason about
+; how to handle them correctly and the Alias Set Tracker models some of them
+; only as Unknown Instructions, which we do not know how to handle either.;
+;
+; CHECK-NOT: Valid
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+@global = external global i64, align 8
+
+declare void @foo55()
+
+define void @blam107() {
+bb:
+  br label %bb1
+
+bb1:                                              ; preds = %bb
+  %tmp = load atomic i8, i8* bitcast (i64* @global to i8*) acquire, align 8
+  br i1 false, label %bb2, label %bb3
+
+bb2:                                              ; preds = %bb1
+  tail call void @foo55() #6
+  br label %bb3
+
+bb3:                                              ; preds = %bb2, %bb1
+  unreachable
+}
+