Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
clang++: error: no such file or directory: 'test_jump_table.cpp'
clang++: error: no input files
62 changes: 62 additions & 0 deletions llvm/include/llvm/Transforms/IPO/JumpTableFinder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// #ifndef LLVM_ANALYSIS_JUMPTABLEFINDERPASS_H
// #define LLVM_ANALYSIS_JUMPTABLEFINDERPASS_H

// #include "llvm/IR/PassManager.h"
// #include "llvm/IR/Module.h"
// #include "llvm/Support/raw_ostream.h"
// #include <set>

// namespace llvm {

// class JumptableFinderPass : public PassInfoMixin<JumptableFinderPass> {
// public:
// /// Main entry point for the pass. Analyzes the module to find and analyze jump tables.
// PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
// /// Implementation of the jump table finder.
// void jumptableFinderImpl(Module &M);

// /// Analyze a SwitchInst for potential jump table patterns.
// void findJumpTableFromSwitch(SwitchInst *SI);

// /// Analyze a GetElementPtrInst for jump table patterns.
// void analyzeJumpTable(GetElementPtrInst *GEP);

// /// Analyze the index computation of a jump table.
// void analyzeIndex(Value *Index);

// /// Find all potential targets for a jump table.
// void findTargets(GetElementPtrInst *GEP, std::set<BasicBlock*> &Targets);

// /// Check the density of a SwitchInst's cases to determine if it forms a jump table.
// bool checkDensity(SwitchInst *SI);

// /// Check if a GetElementPtrInst leads to an indirect branch.
// bool leadsToIndirectBranch(GetElementPtrInst *GEP);
// };

// } // namespace llvm

// #endif // LLVM_ANALYSIS_JUMPTABLEFINDERPASS_H

#ifndef LLVM_TRANSFORMS_IPO_JUMPTABLEFINDER_H
#define LLVM_TRANSFORMS_IPO_JUMPTABLEFINDER_H

#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h" // For PassInfoMixin and PreservedAnalyses
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Constants.h"
#include "llvm/Support/raw_ostream.h"
#include <set>

namespace llvm {

class JumptableFinderPass : public PassInfoMixin<JumptableFinderPass> {
public:
// Entry point for the pass
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

} // namespace llvm

#endif // LLVM_TRANSFORMS_IPO_JUMPTABLEFINDER_H
22 changes: 17 additions & 5 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2862,6 +2862,7 @@ void AsmPrinter::emitJumpTableInfo() {
// Pick the directive to use to print the jump table entries, and switch to
// the appropriate section.
const Function &F = MF->getFunction();
// errs() << "F" << F.getName() << "\n";
const TargetLoweringObjectFile &TLOF = getObjFileLowering();
bool JTInDiffSection = !TLOF.shouldPutJumpTableInFunctionSection(
MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 ||
Expand All @@ -2882,27 +2883,34 @@ void AsmPrinter::emitJumpTableInfo() {

for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) {
const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;


// If this jump table was deleted, ignore it.
if (JTBBs.empty()) continue;
// errs() <<"test0" << "\n";

// For the EK_LabelDifference32 entry, if using .set avoids a relocation,
/// emit a .set directive for each unique entry.
if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 &&
MAI->doesSetDirectiveSuppressReloc()) {
// errs() << "test1" <<"\n";
SmallPtrSet<const MachineBasicBlock*, 16> EmittedSets;
const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF,JTI,OutContext);
for (const MachineBasicBlock *MBB : JTBBs) {
if (!EmittedSets.insert(MBB).second)
continue;

// errs() <<"test2" <<"\n";
// .set LJTSet, LBB32-base
MCSymbol *MCsy = MBB->getSymbol();
MCSymbol *JTsetsy = GetJTSetSymbol(JTI, MBB->getNumber());
const MCExpr *LHS =
MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
OutStreamer->emitAssignment(GetJTSetSymbol(JTI, MBB->getNumber()),
MCSymbolRefExpr::create(MCsy, OutContext);
OutStreamer->emitAssignment(JTsetsy,
MCBinaryExpr::createSub(LHS, Base,
OutContext));
// errs() << "Symbol" << MCsy->getName() << "\n";
// errs() << "JTSymbol" << JTsetsy->getName() << "\n";
}
}

Expand All @@ -2921,8 +2929,10 @@ void AsmPrinter::emitJumpTableInfo() {

// Defer MCAssembler based constant folding due to a performance issue. The
// label differences will be evaluated at write time.
for (const MachineBasicBlock *MBB : JTBBs)
for (const MachineBasicBlock *MBB : JTBBs){
// errs() <<"test6" << "\n";
emitJumpTableEntry(MJTI, MBB, JTI);
}
}

if (EmitJumpTableSizesSection)
Expand Down Expand Up @@ -2987,6 +2997,7 @@ void AsmPrinter::emitJumpTableEntry(const MachineJumpTableInfo *MJTI,
unsigned UID) const {
assert(MBB && MBB->getNumber() >= 0 && "Invalid basic block");
const MCExpr *Value = nullptr;
// errs() << "EntryKind:" << MJTI->getEntryKind() <<"\n";
switch (MJTI->getEntryKind()) {
case MachineJumpTableInfo::EK_Inline:
llvm_unreachable("Cannot emit EK_Inline jump table entry");
Expand Down Expand Up @@ -3026,9 +3037,10 @@ void AsmPrinter::emitJumpTableEntry(const MachineJumpTableInfo *MJTI,
// If the .set directive avoids relocations, this is emitted as:
// .set L4_5_set_123, LBB123 - LJTI1_2
// .word L4_5_set_123
MCSymbol *JTsetsy = GetJTSetSymbol(UID, MBB->getNumber());
if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 &&
MAI->doesSetDirectiveSuppressReloc()) {
Value = MCSymbolRefExpr::create(GetJTSetSymbol(UID, MBB->getNumber()),
Value = MCSymbolRefExpr::create(JTsetsy,
OutContext);
break;
}
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/X86/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tablegen(LLVM X86GenFoldTables.inc -gen-x86-fold-tables -asmwriternum=1)
add_public_tablegen_target(X86CommonTableGen)

set(sources
X86MatchJumptablePass.cpp
X86ArgumentStackSlotRebase.cpp
X86AsmPrinter.cpp
X86AvoidTrailingCall.cpp
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/X86/X86.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ FunctionPass *createX86IndirectBranchTrackingPass();
/// This will prevent a stall when returning on the Atom.
FunctionPass *createX86PadShortFunctions();

FunctionPass *createX86MatchJumptablePass();

/// Return a pass that selectively replaces certain instructions (like add,
/// sub, inc, dec, some shifts, and some multiplies) by equivalent LEA
/// instructions, in order to eliminate execution delays in some processors.
Expand Down
Loading