Preliminary PIC JIT support for X86 (32-bit) / Darwin.
[oota-llvm.git] / lib / CodeGen / BranchFolding.cpp
index d0dcc708d065f075a7e3b6aae338476b9e6b04ce..0f8c6629bae086a9c5c8d885b205835301a301cb 100644 (file)
@@ -46,7 +46,7 @@ namespace {
 
   struct BranchFolder : public MachineFunctionPass {
     static char ID;
-    BranchFolder(bool defaultEnableTailMerge) : 
+    explicit BranchFolder(bool defaultEnableTailMerge) : 
         MachineFunctionPass((intptr_t)&ID) {
           switch (FlagEnableTailMerge) {
           case cl::BOU_UNSET: EnableTailMerge = defaultEnableTailMerge; break;
@@ -271,7 +271,13 @@ static unsigned ComputeCommonTailLength(MachineBasicBlock *MBB1,
   unsigned TailLen = 0;
   while (I1 != MBB1->begin() && I2 != MBB2->begin()) {
     --I1; --I2;
-    if (!I1->isIdenticalTo(I2)) {
+    if (!I1->isIdenticalTo(I2) || 
+        // FIXME: This check is dubious. It's used to get around a problem where
+        // people incorrectly expect inline asm directives to remain in the same
+        // relative order. This is untenable because normal compiler
+        // optimizations (like this one) may reorder and/or merge these
+        // directives.
+        I1->getOpcode() == TargetInstrInfo::INLINEASM) {
       ++I1; ++I2;
       break;
     }
@@ -429,8 +435,14 @@ static bool MergeCompare(const std::pair<unsigned,MachineBasicBlock*> &p,
       return true;
     else if (p.second->getNumber() > q.second->getNumber())
       return false;
-    else
+    else {
+      // _GLIBCXX_DEBUG checks strict weak ordering, which involves comparing
+      // an object with itself.
+#ifndef _GLIBCXX_DEBUG
       assert(0 && "Predecessor appears twice");
+#endif
+      return(false);
+    }
 }
 
 // See if any of the blocks in MergePotentials (which all have a common single
@@ -763,6 +775,11 @@ static bool IsBetterFallthrough(MachineBasicBlock *MBB1,
   // optimize branches that branch to either a return block or an assert block
   // into a fallthrough to the return.
   if (MBB1->empty() || MBB2->empty()) return false;
+  // If there is a clear successor ordering we make sure that one block
+  // will fall through to the next
+  if (MBB1->isSuccessor(MBB2)) return true;
+  if (MBB2->isSuccessor(MBB1)) return false;
 
   MachineInstr *MBB1I = --MBB1->end();
   MachineInstr *MBB2I = --MBB2->end();