Fix PIC jump table codegen on x86-32/linux. In fact, such thing should be applied
authorAnton Korobeynikov <asl@math.spbu.ru>
Wed, 14 Nov 2007 09:18:41 +0000 (09:18 +0000)
committerAnton Korobeynikov <asl@math.spbu.ru>
Wed, 14 Nov 2007 09:18:41 +0000 (09:18 +0000)
to all targets uses GOT-relative offsets for PIC (Alpha?)

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44108 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/AsmPrinter.h
lib/CodeGen/AsmPrinter.cpp
lib/Target/X86/X86ATTAsmPrinter.cpp
lib/Target/X86/X86ATTAsmPrinter.h
test/CodeGen/X86/test-pic-jtbl.ll

index 937e9ed0f0d5b7a80f61b34caabcc931ebc278d2..ceff9b5bd7c380d637afdad3e95318c35eab34d1 100644 (file)
@@ -311,7 +311,10 @@ namespace llvm {
                                            const MachineBasicBlock *MBB) const;
     virtual void printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
                                            const MachineBasicBlock *MBB) const;
-
+    virtual void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
+                                        const MachineBasicBlock *MBB,
+                                        unsigned uid) const;
+    
     /// printDataDirective - This method prints the asm directive for the
     /// specified type.
     void printDataDirective(const Type *type);
index ed51e10d220055b742686ddb51303d613b2c7aac..ce89337f36fbf2c4f3e83dd299fd3d0b86e726c1 100644 (file)
@@ -245,17 +245,9 @@ void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
                                    MachineFunction &MF) {
   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
   if (JT.empty()) return;
+
   bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
   
-  // Use JumpTableDirective otherwise honor the entry size from the jump table
-  // info.
-  const char *JTEntryDirective = TAI->getJumpTableDirective();
-  bool HadJTEntryDirective = JTEntryDirective != NULL;
-  if (!HadJTEntryDirective) {
-    JTEntryDirective = MJTI->getEntrySize() == 4 ?
-      TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
-  }
-  
   // Pick the directive to use to print the jump table entries, and switch to 
   // the appropriate section.
   TargetLowering *LoweringInfo = TM.getTargetLowering();
@@ -300,30 +292,51 @@ void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
       << '_' << i << ":\n";
     
     for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
-      O << JTEntryDirective << ' ';
-      // If we have emitted set directives for the jump table entries, print 
-      // them rather than the entries themselves.  If we're emitting PIC, then
-      // emit the table entries as differences between two text section labels.
-      // If we're emitting non-PIC code, then emit the entries as direct
-      // references to the target basic blocks.
-      if (!EmittedSets.empty()) {
-        O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
-          << '_' << i << "_set_" << JTBBs[ii]->getNumber();
-      } else if (IsPic) {
-        printBasicBlockLabel(JTBBs[ii], false, false);
-        // If the arch uses custom Jump Table directives, don't calc relative to
-        // JT
-        if (!HadJTEntryDirective) 
-          O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
-            << getFunctionNumber() << '_' << i;
-      } else {
-        printBasicBlockLabel(JTBBs[ii], false, false);
-      }
+      printPICJumpTableEntry(MJTI, JTBBs[ii], i);
       O << '\n';
     }
   }
 }
 
+void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
+                                        const MachineBasicBlock *MBB,
+                                        unsigned uid)  const {
+  bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
+  
+  // Use JumpTableDirective otherwise honor the entry size from the jump table
+  // info.
+  const char *JTEntryDirective = TAI->getJumpTableDirective();
+  bool HadJTEntryDirective = JTEntryDirective != NULL;
+  if (!HadJTEntryDirective) {
+    JTEntryDirective = MJTI->getEntrySize() == 4 ?
+      TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
+  }
+
+  O << JTEntryDirective << ' ';
+
+  // If we have emitted set directives for the jump table entries, print 
+  // them rather than the entries themselves.  If we're emitting PIC, then
+  // emit the table entries as differences between two text section labels.
+  // If we're emitting non-PIC code, then emit the entries as direct
+  // references to the target basic blocks.
+  if (IsPic) {
+    if (TAI->getSetDirective()) {
+      O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
+        << '_' << uid << "_set_" << MBB->getNumber();
+    } else {
+      printBasicBlockLabel(MBB, false, false);
+      // If the arch uses custom Jump Table directives, don't calc relative to
+      // JT
+      if (!HadJTEntryDirective) 
+        O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
+          << getFunctionNumber() << '_' << uid;
+    }
+  } else {
+    printBasicBlockLabel(MBB, false, false);
+  }
+}
+
+
 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
 /// special global used by LLVM.  If so, emit it and return true, otherwise
 /// do nothing and return false.
index f46ebb840c203c4bf7154d8f126fc6d0fb1b1243..44040977fed7e0eb16126f9b0f1f8c81f8801dee 100644 (file)
@@ -22,6 +22,7 @@
 #include "X86TargetAsmInfo.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/CallingConv.h"
+#include "llvm/CodeGen/MachineJumpTableInfo.h"
 #include "llvm/Module.h"
 #include "llvm/Support/Mangler.h"
 #include "llvm/Target/TargetAsmInfo.h"
@@ -502,7 +503,11 @@ void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
                                            const MachineBasicBlock *MBB) const {
   if (!TAI->getSetDirective())
     return;
-  
+
+  // We don't need .set machinery if we have GOT-style relocations
+  if (Subtarget->isPICStyleGOT())
+    return;
+    
   O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
     << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
   printBasicBlockLabel(MBB, false, false);
@@ -519,6 +524,28 @@ void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
 }
 
 
+void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
+                                              const MachineBasicBlock *MBB,
+                                              unsigned uid) const 
+{  
+  const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
+    TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
+
+  O << JTEntryDirective << ' ';
+
+  if (TM.getRelocationModel() == Reloc::PIC_) {
+    if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
+      O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
+        << '_' << uid << "_set_" << MBB->getNumber();
+    } else if (Subtarget->isPICStyleGOT()) {
+      printBasicBlockLabel(MBB, false, false);
+      O << "@GOTOFF";
+    } else
+      assert(0 && "Don't know how to print MBB label for this PIC mode");
+  } else
+    printBasicBlockLabel(MBB, false, false);
+}
+
 bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
                                          const char Mode) {
   const MRegisterInfo &RI = *TM.getRegisterInfo();
index d1e036edd630c417f40476b814bc55833631a3f3..225a71389f79209f5772eb8da4bdb7c9db278cd2 100644 (file)
@@ -19,6 +19,8 @@
 
 namespace llvm {
 
+struct MachineJumpTableInfo;
+    
 struct VISIBILITY_HIDDEN X86ATTAsmPrinter : public X86SharedAsmPrinter {
  X86ATTAsmPrinter(std::ostream &O, X86TargetMachine &TM, const TargetAsmInfo *T)
     : X86SharedAsmPrinter(O, TM, T) { }
@@ -83,6 +85,10 @@ struct VISIBILITY_HIDDEN X86ATTAsmPrinter : public X86SharedAsmPrinter {
                                  const MachineBasicBlock *MBB) const {
     AsmPrinter::printPICJumpTableSetLabel(uid, uid2, MBB);
   }
+  void printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
+                              const MachineBasicBlock *MBB,
+                              unsigned uid) const;
+  
   void printPICLabel(const MachineInstr *MI, unsigned Op);
   bool runOnMachineFunction(MachineFunction &F);
   
index 516a20bfb5c9bdd211440207d0d9a7f2b8fb0639..e23f7c1a9fc23bf882b36ddc54dec5e96b371de9 100644 (file)
@@ -1,9 +1,9 @@
 ; RUN: llvm-as < %s | llc -mtriple=i686-pc-linux-gnu -relocation-model=pic \
 ; RUN:   -o %t -f
 ; RUN: grep _GLOBAL_OFFSET_TABLE_ %t
-; RUN: grep piclabel %t | count 9
+; RUN: grep piclabel %t | count 3
 ; RUN: grep PLT %t | count 6
-; RUN: grep GOTOFF %t | count 1
+; RUN: grep GOTOFF %t | count 14
 ; RUN: grep JTI %t | count 2
 
 define void @bar(i32 %n.u) {