Converts relaxed loads to acquire loads when necessary
authorPeizhao Ou <peizhaoo@uci.edu>
Mon, 2 Apr 2018 22:57:20 +0000 (15:57 -0700)
committerPeizhao Ou <peizhaoo@uci.edu>
Mon, 2 Apr 2018 22:57:20 +0000 (15:57 -0700)
lib/CodeGen/CodeGenPrepare.cpp

index f4ff3787e630347a89b3737db4ebb16239df75e5..3eaeaa2360c9ab89e434faa9053c887b98f519a7 100644 (file)
@@ -1016,6 +1016,41 @@ bool AddFakeConditionalBranchAfterMonotonicLoads(
   return Changed;
 }
 
+// XXX-comment: Returns whether the code has been changed.
+bool ConvertMonotonicLoadsToAcquire(SmallSet<LoadInst*, 1>& MonotonicLoadInsts)
+{
+  bool Changed = false;
+  while (!MonotonicLoadInsts.empty()) {
+    auto* LI = *MonotonicLoadInsts.begin();
+    MonotonicLoadInsts.erase(LI);
+    SmallVector<BasicBlock*, 2> ChainedBB;
+    auto* FirstInst = findFirstStoreCondBranchInst(LI, &ChainedBB);
+    if (FirstInst != nullptr) {
+      if (FirstInst->getOpcode() == Instruction::Store) {
+        if (StoreAddressDependOnValue(dyn_cast<StoreInst>(FirstInst), LI)) {
+          continue;
+        }
+      } else if (FirstInst->getOpcode() == Instruction::Br) {
+        if (ConditionalBranchDependsOnValue(dyn_cast<BranchInst>(FirstInst),
+                                            LI)) {
+          continue;
+        }
+      } else {
+        IntrinsicInst* II = dyn_cast<IntrinsicInst>(FirstInst);
+        if (!II || II->getIntrinsicID() != Intrinsic::aarch64_stlxr) {
+          dbgs() << "FirstInst=" << *FirstInst << "\n";
+          assert(false && "findFirstStoreCondBranchInst() should return a "
+                          "store/condition branch instruction");
+        }
+      }
+    }
+
+    // We really need convert the load to acquire.
+    LI->setOrdering(Acquire);
+  }
+  return Changed;
+}
+
 /**** Implementations of public methods for dependence tainting ****/
 Value* GetUntaintedAddress(Value* CurrentAddress) {
   auto* OrAddress = getOrAddress(CurrentAddress);
@@ -1406,8 +1441,7 @@ bool CodeGenPrepare::runOnFunction(Function &F) {
       }
     }
   }
-  EverMadeChange |=
-      AddFakeConditionalBranchAfterMonotonicLoads(MonotonicLoadInsts, DT);
+  EverMadeChange |= ConvertMonotonicLoadsToAcquire(MonotonicLoadInsts);
 
   return EverMadeChange;
 }