Skip to content

Instantly share code, notes, and snippets.

@TimSimpson
Created October 5, 2013 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimSimpson/6842685 to your computer and use it in GitHub Desktop.
Save TimSimpson/6842685 to your computer and use it in GitHub Desktop.
These are two diff files created by Aditya Kumar for his presentation on the LLVM project (see here for more info http://www.meetup.com/The-Austin-C-C-Meetup-Group/events/142294062/). From Aditya's notes: """ The clang.diff file can be applied directly as is. The llvm.diff file contains changes to be made to the existing source files as well as …
diff --git a/examples/Makefile b/examples/Makefile
index d8d9028..700014b 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -9,6 +9,6 @@
CLANG_LEVEL := ..
-PARALLEL_DIRS := analyzer-plugin clang-interpreter PrintFunctionNames
+PARALLEL_DIRS := analyzer-plugin PrintFunctionNames
include $(CLANG_LEVEL)/Makefile
diff --git a/examples/PrintFunctionNames/PrintFunctionNames.cpp b/examples/PrintFunctionNames/PrintFunctionNames.cpp
index f6e75cc..04d7249 100644
--- a/examples/PrintFunctionNames/PrintFunctionNames.cpp
+++ b/examples/PrintFunctionNames/PrintFunctionNames.cpp
@@ -26,10 +26,21 @@ public:
virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
const Decl *D = *i;
- if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
- llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
- }
-
+ if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
+ llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"";
+ if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
+ llvm::errs() << "\tFunction declaration:" << FD->getNameAsString();
+ if (FD->isGlobal()) {
+ llvm::errs() << "\t[Global function]";
+ }
+ }
+ if (const VarDecl * VD = dyn_cast<VarDecl>(D))
+ llvm::errs() << "\t[Don't use a global variable]";
+ if (const RecordDecl* RD = dyn_cast<RecordDecl>(ND))
+ llvm::errs() << "\t[class declaration]";
+ }
+ }
+ llvm::errs() << "\n";
return true;
}
};
diff --git a/lib/Target/X86/X86.h b/lib/Target/X86/X86.h
index 947002f..44215a1 100644
--- a/lib/Target/X86/X86.h
+++ b/lib/Target/X86/X86.h
@@ -75,6 +75,10 @@ FunctionPass *createX86PadShortFunctions();
/// to eliminate execution delays in some Atom processors.
FunctionPass *createX86FixupLEAs();
+/// Add a declaration of the externally visible function that will be called
+/// get an instance of the pass.
+FunctionPass *createX86MyPass();
+
} // End llvm namespace
#endif
diff --git a/lib/Target/X86/X86TargetMachine.cpp b/lib/Target/X86/X86TargetMachine.cpp
index 81423a3..5baa1da 100644
--- a/lib/Target/X86/X86TargetMachine.cpp
+++ b/lib/Target/X86/X86TargetMachine.cpp
@@ -124,6 +124,11 @@ static cl::opt<bool>
X86EarlyIfConv("x86-early-ifcvt",
cl::desc("Enable early if-conversion on X86"));
+static cl::opt<bool>
+EnableMyPass("x86-enable-mypass",
+ cl::desc("Enable my pass"),
+ cl::init(true));
+
//===----------------------------------------------------------------------===//
// X86 Analysis Pass Setup
//===----------------------------------------------------------------------===//
@@ -197,6 +202,11 @@ bool X86PassConfig::addPreRegAlloc() {
bool X86PassConfig::addPostRegAlloc() {
addPass(createX86FloatingPointStackifierPass());
+ // Added the pass you want to run at appropriate place.
+ if (EnableMyPass) {
+ addPass(createX86MyPass());
+ printAndVerify("After My Pass");
+ }
return true; // -print-machineinstr should print after this.
}
//===-- MyPass.cpp - How to write an LLVM Backend pass -----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Summarize:
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "x86-vzeroupper"
#include "X86.h"
#include "X86InstrInfo.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetInstrInfo.h"
using namespace llvm;
namespace {
struct MyPass : public MachineFunctionPass {
static char ID;
MyPass() : MachineFunctionPass(ID) {}
virtual bool runOnMachineFunction(MachineFunction &MF);
bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB);
virtual const char *getPassName() const { return "X86 vzeroupper inserter";}
private:
const TargetInstrInfo *TII; // Machine instruction info.
};
char MyPass::ID = 0;
}
FunctionPass *llvm::createX86MyPass() {
return new MyPass();
}
/// runOnMachineFunction - Loop over all of the basic blocks
bool MyPass::runOnMachineFunction(MachineFunction &MF) {
TII = MF.getTarget().getInstrInfo();
MachineRegisterInfo &MRI = MF.getRegInfo();
DEBUG(dbgs() << "\nRunning MyPass");
for (MachineFunction::iterator MBBI = MF.begin(), MBBE = MF.end();
MBBI != MBBE; ++MBBI) {
DEBUG(dbgs() << "\nIn basic block:" << MBBI->getNumber());
for (MachineBasicBlock::instr_iterator MII = MBBI->instr_begin(), E = MBBI->instr_end();
MII != E; ++MII) {
DEBUG(dbgs() << "\n"; MII->dump());
for (unsigned i = 0; i < MII->getNumOperands(); ++i) {
MachineOperand& MO = MII->getOperand(i);
if (MO.isReg() && MO.isKill())
MO.setIsKill(false);
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment