Much improved pic jumptable codegen:

Then:
        call    "L1$pb"
"L1$pb":
        popl    %eax
		...
LBB1_1: # entry
        imull   $4, %ecx, %ecx
        leal    LJTI1_0-"L1$pb"(%eax), %edx
        addl    LJTI1_0-"L1$pb"(%ecx,%eax), %edx
        jmpl    *%edx

        .align  2
        .set L1_0_set_3,LBB1_3-LJTI1_0
        .set L1_0_set_2,LBB1_2-LJTI1_0
        .set L1_0_set_5,LBB1_5-LJTI1_0
        .set L1_0_set_4,LBB1_4-LJTI1_0
LJTI1_0:
        .long    L1_0_set_3
        .long    L1_0_set_2

Now:
        call    "L1$pb"
"L1$pb":
        popl    %eax
		...
LBB1_1: # entry
        addl    LJTI1_0-"L1$pb"(%eax,%ecx,4), %eax
        jmpl    *%eax

		.align  2
		.set L1_0_set_3,LBB1_3-"L1$pb"
		.set L1_0_set_2,LBB1_2-"L1$pb"
		.set L1_0_set_5,LBB1_5-"L1$pb"
		.set L1_0_set_4,LBB1_4-"L1$pb"
LJTI1_0:
        .long    L1_0_set_3
        .long    L1_0_set_2


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43924 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter.cpp
index 3500d12..ed51e10 100644
--- a/lib/CodeGen/AsmPrinter.cpp
+++ b/lib/CodeGen/AsmPrinter.cpp
@@ -26,6 +26,7 @@
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetLowering.h"
 #include "llvm/Target/TargetMachine.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include <cerrno>
 using namespace llvm;
 
@@ -282,11 +283,11 @@
     // For PIC codegen, if possible we want to use the SetDirective to reduce
     // the number of relocations the assembler will generate for the jump table.
     // Set directives are all printed before the jump table itself.
-    std::set<MachineBasicBlock*> EmittedSets;
+    SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
     if (TAI->getSetDirective() && IsPic)
       for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
-        if (EmittedSets.insert(JTBBs[ii]).second)
-          printSetLabel(i, JTBBs[ii]);
+        if (EmittedSets.insert(JTBBs[ii]))
+          printPICJumpTableSetLabel(i, JTBBs[ii]);
     
     // On some targets (e.g. darwin) we want to emit two consequtive labels
     // before each jump table.  The first label is never referenced, but tells
@@ -1256,10 +1257,10 @@
       << MBB->getBasicBlock()->getName();
 }
 
-/// printSetLabel - This method prints a set label for the specified
-/// MachineBasicBlock
-void AsmPrinter::printSetLabel(unsigned uid, 
-                               const MachineBasicBlock *MBB) const {
+/// printPICJumpTableSetLabel - This method prints a set label for the
+/// specified MachineBasicBlock for a jumptable entry.
+void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, 
+                                           const MachineBasicBlock *MBB) const {
   if (!TAI->getSetDirective())
     return;
   
@@ -1270,8 +1271,8 @@
     << '_' << uid << '\n';
 }
 
-void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
-                               const MachineBasicBlock *MBB) const {
+void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
+                                           const MachineBasicBlock *MBB) const {
   if (!TAI->getSetDirective())
     return;
   
diff --git a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
index 6e58631..0731299 100644
--- a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
+++ b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
@@ -1599,21 +1599,17 @@
       case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
       }
 
+      Addr = LD;
       if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
         // For PIC, the sequence is:
         // BRIND(load(Jumptable + index) + RelocBase)
-        // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
-        SDOperand Reloc;
-        if (TLI.usesGlobalOffsetTable())
-          Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
-        else
-          Reloc = Table;
-        Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
-        Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
-        Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
-      } else {
-        Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
+        // RelocBase can be JumpTable, GOT or some sort of global base.
+        if (PTy != MVT::i32)
+          Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
+        Addr = DAG.getNode(ISD::ADD, PTy, Addr,
+                           TLI.getPICJumpTableRelocBase(Table, DAG));
       }
+      Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
     }
     }
     break;
diff --git a/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 3708b1c..eadfa1f 100644
--- a/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -393,6 +393,13 @@
   return 1;
 }
 
+SDOperand TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
+                                                   SelectionDAG &DAG) const {
+  if (usesGlobalOffsetTable())
+    return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
+  return Table;
+}
+
 //===----------------------------------------------------------------------===//
 //  Optimization Methods
 //===----------------------------------------------------------------------===//