[llvm-commits] [poolalloc] r125692 - in /poolalloc/trunk/lib/AssistDS: MergeGEP.cpp VarArgsFunc.cpp
Arushi Aggarwal
aggarwa4 at illinois.edu
Wed Feb 16 15:19:40 PST 2011
Author: aggarwa4
Date: Wed Feb 16 17:19:40 2011
New Revision: 125692
URL: http://llvm.org/viewvc/llvm-project?rev=125692&view=rev
Log:
Helper functions for DSA.
MergeGEP helps merge GEPs. This is similiar to
some level of instruction combining.
VarargsFunc, helps convert calls of type
call (bitcast F(void) to (...)*)() to direct calls to
F, without the bitcast.
Added:
poolalloc/trunk/lib/AssistDS/MergeGEP.cpp
poolalloc/trunk/lib/AssistDS/VarArgsFunc.cpp
Added: poolalloc/trunk/lib/AssistDS/MergeGEP.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/MergeGEP.cpp?rev=125692&view=auto
==============================================================================
--- poolalloc/trunk/lib/AssistDS/MergeGEP.cpp (added)
+++ poolalloc/trunk/lib/AssistDS/MergeGEP.cpp Wed Feb 16 17:19:40 2011
@@ -0,0 +1,125 @@
+//===-- MergeGEP.cpp - Merge GEPs for indexing in arrays ------------ ----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//
+//===----------------------------------------------------------------------===//
+#define DEBUG_TYPE "mergegep"
+
+#include "llvm/Instructions.h"
+#include "llvm/Module.h"
+#include "llvm/Pass.h"
+#include "llvm/Instructions.h"
+#include "llvm/Constants.h"
+#include "llvm/Support/GetElementPtrTypeIterator.h"
+#include "llvm/Transforms/Utils/Cloning.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/Support/FormattedStream.h"
+#include "llvm/Support/Debug.h"
+#include <vector>
+
+using namespace llvm;
+
+
+namespace {
+ class MergeGEP : public ModulePass {
+ public:
+ static char ID;
+ MergeGEP() : ModulePass(&ID) {}
+ bool runOnModule(Module& M) {
+ bool changed = false;
+ bool found;
+ do {
+ found = false;
+ for (Module::iterator F = M.begin(); F != M.end(); ++F){
+ for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
+ for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE; I++) {
+ if(!(isa<GetElementPtrInst>(I)))
+ continue;
+ GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
+ if(!isa<ArrayType>(GEP->getType()->getElementType()))
+ continue;
+ for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
+ I != E; ++I) {
+ if (isa<StructType>(*I)) {
+ gep_type_iterator II = I;
+ if(++II == E){
+ std::vector<GetElementPtrInst*> worklist;
+ for (Value::use_iterator UI = GEP->use_begin(),
+ UE = GEP->use_end(); UI != UE; ++UI){
+ if(!isa<GetElementPtrInst>(UI))
+ break;
+ GetElementPtrInst *GEPUse = cast<GetElementPtrInst>(UI);
+ worklist.push_back(GEPUse);
+ }
+ while(!worklist.empty()){
+ GetElementPtrInst *GEPUse = worklist.back();
+ worklist.pop_back();
+ SmallVector<Value*, 8> Indices;
+ Indices.append(GEP->op_begin()+1, GEP->op_end());
+ Indices.append(GEPUse->idx_begin()+1, GEPUse->idx_end());
+ GetElementPtrInst *GEPNew = GetElementPtrInst::Create(GEP->getOperand(0),
+ Indices.begin(),
+ Indices.end(),
+ GEPUse->getName()+ "mod",
+ GEPUse);
+ GEPUse->replaceAllUsesWith(GEPNew);
+ GEPUse->eraseFromParent();
+ found = true;
+ changed = true;
+ }
+ }
+ }
+ }
+ }
+ }
+ std::vector<GetElementPtrInst*> worklist;
+ for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
+ for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE; I++) {
+ if(!(isa<GetElementPtrInst>(I)))
+ continue;
+ GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
+ if (Constant *C = dyn_cast<Constant>(GEP->getOperand(0))) {
+ if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
+ if (CE->getOpcode() == Instruction::GetElementPtr) {
+ worklist.push_back(GEP);
+ }
+ }
+ }
+ }
+ }
+ while(!worklist.empty()) {
+ GetElementPtrInst *GEP = worklist.back();
+ worklist.pop_back();
+ Constant *C = cast<Constant>(GEP->getOperand(0));
+ ConstantExpr *CE = cast<ConstantExpr>(C);
+ SmallVector<Value*, 8> Indices;
+ Indices.append(CE->op_begin()+1, CE->op_end());
+ Indices.append(GEP->idx_begin()+1, GEP->idx_end());
+ GetElementPtrInst *GEPNew = GetElementPtrInst::Create(CE->getOperand(0),
+ Indices.begin(),
+ Indices.end(),
+ GEP->getName()+ "mod",
+ GEP);
+ //GEP->dump();
+ //GEPNew->dump();
+ GEP->replaceAllUsesWith(GEPNew);
+ GEP->eraseFromParent();
+ changed = true;
+ found = true;
+ }
+ }
+ }while(found);
+ return changed;
+ }
+ };
+}
+
+char MergeGEP::ID = 0;
+static RegisterPass<MergeGEP>
+X("mergegep", "Merge GEPs for arrays in structs");
Added: poolalloc/trunk/lib/AssistDS/VarArgsFunc.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/VarArgsFunc.cpp?rev=125692&view=auto
==============================================================================
--- poolalloc/trunk/lib/AssistDS/VarArgsFunc.cpp (added)
+++ poolalloc/trunk/lib/AssistDS/VarArgsFunc.cpp Wed Feb 16 17:19:40 2011
@@ -0,0 +1,93 @@
+//===-- VarArgsFunc.cpp - Simplify calls to bitcasted const funcs --------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+#define DEBUG_TYPE "varargfunc"
+
+#include "llvm/Instructions.h"
+#include "llvm/Module.h"
+#include "llvm/Pass.h"
+#include "llvm/Transforms/Utils/Cloning.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/Support/FormattedStream.h"
+#include "llvm/Support/Debug.h"
+
+#include <set>
+#include <map>
+#include <vector>
+
+using namespace llvm;
+
+STATISTIC(numSimplified, "Number of Calls Simplified");
+
+namespace {
+ class VarArgsFunc : public ModulePass {
+ public:
+ static char ID;
+ VarArgsFunc() : ModulePass(&ID) {}
+ bool runOnModule(Module& M) {
+ bool changed = false;
+ std::vector<CallInst*> worklist;
+ for (Module::iterator I = M.begin(); I != M.end(); ++I) {
+ if (!I->isDeclaration() && !I->mayBeOverridden()) {
+ //Call Sites
+ for(Value::use_iterator ui = I->use_begin(), ue = I->use_end();
+ ui != ue; ++ui) {
+ if (Constant *C = dyn_cast<Constant>(ui)) {
+ if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
+ if (CE->getOpcode() == Instruction::BitCast) {
+ if(CE->getOperand(0) == I) {
+ if(const FunctionType *FTy = dyn_cast<FunctionType>((cast<PointerType>(CE->getType()))->getElementType())) {
+ if(FTy->isVarArg()) {
+ for(Value::use_iterator uii = CE->use_begin(), uee = CE->use_end();
+ uii != uee; ++uii) {
+ if (CallInst* CI = dyn_cast<CallInst>(uii)) {
+ if(CI->getCalledValue() == CE) {
+ worklist.push_back(CI);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ while(!worklist.empty()) {
+ CallInst *CI = worklist.back();
+ worklist.pop_back();
+ Function *F = cast<Function>(CI->getCalledValue()->stripPointerCasts());
+ if(F->arg_size() != (CI->getNumOperands()-1))
+ continue;
+ if(F->getReturnType() != CI->getType())
+ continue;
+ int arg_count = 1;
+ bool change = true;
+ for (Function::arg_iterator ii = F->arg_begin(), ee = F->arg_end();ii != ee; ++ii,arg_count++) {
+ if(ii->getType() != CI->getOperand(arg_count)->getType()) {
+ change = false;
+ break;
+ }
+ }
+
+ if(change) {
+ CI->setCalledFunction(F);
+ changed = true;
+ numSimplified++;
+ }
+ }
+ return changed;
+ }
+ };
+}
+
+char VarArgsFunc::ID = 0;
+static RegisterPass<VarArgsFunc>
+X("varargsfunc", "Specialize for ill-defined non-varargs functions");
More information about the llvm-commits
mailing list