Generates conditional branch instead of fake ones for Select instruction in some...
authorPeizhao Ou <peizhaoo@uci.edu>
Wed, 29 Nov 2017 00:37:38 +0000 (16:37 -0800)
committerPeizhao Ou <peizhaoo@uci.edu>
Wed, 29 Nov 2017 00:37:38 +0000 (16:37 -0800)
include/llvm/CodeGen/MachineBasicBlock.h
include/llvm/CodeGen/MachineInstr.h
lib/CodeGen/CodeGenPrepare.cpp
lib/CodeGen/MachineInstr.cpp
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

index 9585b29a5dc30ce145b6bc63c45019a145471e30..fd786f68fb653b240015fd7cb58d351470dc50f6 100644 (file)
@@ -81,10 +81,6 @@ public:
   };
 
 private:
-  // XXX-update: A flag that checks whether we can eliminate this machine basic
-  // block.
-  bool canEliminateMachineBB;
-
   typedef ilist<MachineInstr> Instructions;
   Instructions Insts;
   const BasicBlock *BB;
@@ -128,6 +124,10 @@ private:
   /// is only computed once and is cached.
   mutable MCSymbol *CachedMCSymbol = nullptr;
 
+  // XXX-update: A flag that checks whether we can eliminate this machine basic
+  // block.
+  bool canEliminateMachineBB;
+
   // Intrusive list support
   MachineBasicBlock() {}
 
index 3e76ea9c31dd7f5e082962855a7338b1d9441686..05c9a9e0b07961a7afdb47980332da1fa0b3494d 100644 (file)
@@ -71,9 +71,6 @@ public:
     BundledSucc  = 1 << 3               // Instruction has bundled successors.
   };
 private:
-  // XXX-update: A flag that checks whether we can eliminate this instruction.
-  bool canEliminateMachineInstr;
-
   const MCInstrDesc *MCID;              // Instruction descriptor.
   MachineBasicBlock *Parent;            // Pointer to the owning basic block.
 
@@ -129,15 +126,6 @@ private:
   friend class MachineFunction;
 
 public:
-  // XXX-update:
-  void disableCanEliminateMachineInstr() {
-    canEliminateMachineInstr = false;
-  }
-
-  bool getCanEliminateMachineInstr() {
-    return canEliminateMachineInstr;
-  }
-
   const MachineBasicBlock* getParent() const { return Parent; }
   MachineBasicBlock* getParent() { return Parent; }
 
index 1a07bfc0c3577f71585153b63635d87173c47479..5c9858dcec2f3cc2cb0975322800992b670f96a1 100644 (file)
@@ -518,6 +518,8 @@ Value* getRootDependence(Value* DepVal) {
 bool taintStoreAddress(StoreInst* SI, Value* DepVal,
                        const char* calling_func = __builtin_FUNCTION()) {
   DEBUG(dbgs() << "Called from " << calling_func << '\n');
+  // Set the insertion point right after the 'DepVal'.
+  Instruction* Inst = nullptr;
   IRBuilder<true, NoFolder> Builder(SI);
   BasicBlock* BB = SI->getParent();
   Value* Address = SI->getPointerOperand();
@@ -749,9 +751,21 @@ void AddFakeConditionalBranch(Instruction* SplitInst, Value* Condition) {
 
 // Returns true if the code is changed, and false otherwise.
 void TaintRelaxedLoads(LoadInst* LI) {
-  IRBuilder<true, NoFolder> Builder(LI->getNextNode());
+  // For better performance, we can add a "AND X 0" instruction before the
+  // condition.
+  auto* FirstInst = findFirstStoreCondBranchInst(LI);
+  Instruction* InsertPoint = nullptr;
+  if (FirstInst == nullptr) {
+    InsertPoint = LI->getParent()->getTerminator();
+    InsertPoint = LI->getNextNode();
+  } else {
+    InsertPoint = LI->getNextNode();
+  }
+  IRBuilder<true, NoFolder> Builder(InsertPoint);
+  auto* AndZero = dyn_cast<Instruction>(
+      Builder.CreateAnd(LI, Constant::getNullValue(LI->getType())));
   auto* FakeCondition = dyn_cast<Instruction>(Builder.CreateICmp(
-      CmpInst::ICMP_EQ, LI, Constant::getNullValue(LI->getType())));
+      CmpInst::ICMP_NE, AndZero, Constant::getNullValue(LI->getType())));
   AddFakeConditionalBranch(FakeCondition->getNextNode(), FakeCondition);
 }
 
@@ -779,8 +793,15 @@ bool AddFakeConditionalBranchAfterMonotonicLoads(
     }
 
     // We really need to process the relaxed load now.
-    TaintRelaxedLoads(LI);
-    Changed = true;
+    StoreInst* SI = nullptr;;
+    if (FirstInst && (SI = dyn_cast<StoreInst>(FirstInst))) {
+      // For immediately coming stores, taint the address of the store.
+      taintStoreAddress(SI, LI);
+    } else {
+      // For immediately coming branch, directly add a fake branch.
+      TaintRelaxedLoads(LI);
+      Changed = true;
+    }
   }
   return Changed;
 }
index 5e542757b6383fce92373cc3cb29580d571720c6..6dca74d60026ff2044bb50a497f1a42d99e7c57e 100644 (file)
@@ -647,7 +647,7 @@ MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
                            DebugLoc dl, bool NoImp)
     : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0),
       AsmPrinterFlags(0), NumMemRefs(0), MemRefs(nullptr),
-      debugLoc(std::move(dl)), canEliminateMachineInstr(true) {
+      debugLoc(std::move(dl)) {
   assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
 
   // Reserve space for the expected number of operands.
@@ -667,7 +667,7 @@ MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
   : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0),
     Flags(0), AsmPrinterFlags(0),
     NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs),
-    debugLoc(MI.getDebugLoc()), canEliminateMachineInstr(true) {
+    debugLoc(MI.getDebugLoc()) {
   assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
 
   CapOperands = OperandCapacity::get(MI.getNumOperands());
index bdcf5a2a89ece0a68e8186b29ed9bd63babf6907..f119023d217b03ea185eb29990623f5f498b1469 100644 (file)
@@ -3101,7 +3101,7 @@ SDValue DAGCombiner::visitAND(SDNode *N) {
 
   // XXX-disabled: (and x, 0) should not be folded.
   // (and (and x, 0), y) shouldn't either.
-  if (!N0C && N1C->isNullValue()) {
+  if (!N0C && N1C && N1C->isNullValue()) {
     return SDValue();
   }
   if (!N0C) {
index 1676e2dfe08fb477b6cb01df9bec642449a880d7..491d7ec451c0525740732541e9e0b2b0625c063a 100644 (file)
@@ -663,11 +663,15 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
     MachineBasicBlock* Succ2Succ = nullptr;
     if ((Succ1->size() == 1 && Succ1->begin()->isUnconditionalBranch()) ||
         (Succ1->size() == 0)) {
-      Succ1Succ = *Succ1->succ_begin();
+      if (Succ1->succ_size()) {
+        Succ1Succ = *Succ1->succ_begin();
+      }
     }
     if ((Succ2->size() == 1 && Succ2->begin()->isUnconditionalBranch()) ||
         (Succ2->size() == 0)) {
-      Succ2Succ = *Succ2->succ_begin();
+      if (Succ1->succ_size()) {
+        Succ2Succ = *Succ2->succ_begin();
+      }
     }
 
     bool HasCommonDest = Succ1Succ && Succ1Succ == Succ2Succ;
@@ -675,7 +679,6 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
       auto MBBIter = MBB.end();
       std::advance(MBBIter, -2);
       assert(MBBIter->isConditionalBranch());
-      MBBIter->disableCanEliminateMachineInstr();
       MBB.disableCanEliminateMachineBB();
       Succ1->disableCanEliminateMachineBB();
       Succ2->disableCanEliminateMachineBB();