Static Value-Flow Analysis
Public Member Functions | Static Public Attributes | List of all members
SVF::BreakConstantGEPs Class Reference

#include <BreakConstantExpr.h>

Inheritance diagram for SVF::BreakConstantGEPs:

Public Member Functions

 BreakConstantGEPs ()
 
llvm::StringRef getPassName () const
 
virtual bool runOnModule (Module &M)
 

Static Public Attributes

static char ID = 0
 

Detailed Description

Definition at line 30 of file BreakConstantExpr.h.

Constructor & Destructor Documentation

◆ BreakConstantGEPs()

SVF::BreakConstantGEPs::BreakConstantGEPs ( )
inline

Definition at line 39 of file BreakConstantExpr.h.

39 : ModulePass(ID) {}
llvm::ModulePass ModulePass
Definition: BasicTypes.h:73

Member Function Documentation

◆ getPassName()

llvm::StringRef SVF::BreakConstantGEPs::getPassName ( ) const
inline

Definition at line 40 of file BreakConstantExpr.h.

41  {
42  return "Remove Constant GEP Expressions";
43  }

◆ runOnModule()

bool BreakConstantGEPs::runOnModule ( Module M)
virtual

Definition at line 186 of file BreakConstantExpr.cpp.

187 {
188  bool modified = false;
189  for (Module::iterator F = module.begin(), E = module.end(); F != E; ++F)
190  {
191  // Worklist of values to check for constant GEP expressions
192  std::vector<Instruction* > Worklist;
193 
194  //
195  // Initialize the worklist by finding all instructions that have one or more
196  // operands containing a constant GEP expression.
197  //
198  for (Function::iterator BB = (*F).begin(); BB != (*F).end(); ++BB)
199  {
200  for (BasicBlock::iterator i = BB->begin(); i != BB->end(); ++i)
201  {
202  //
203  // Scan through the operands of this instruction. If it is a constant
204  // expression GEP, insert an instruction GEP before the instruction.
205  //
206  Instruction* I = &(*i);
207  for (u32_t index = 0; index < I->getNumOperands(); ++index)
208  {
209  if (hasConstantExpr(I->getOperand(index)))
210  {
211  Worklist.push_back (I);
212  }
213  }
214  }
215  }
216 
217  //
218  // Determine whether we will modify anything.
219  //
220  if (Worklist.size()) modified = true;
221 
222  //
223  // While the worklist is not empty, take an item from it, convert the
224  // operands into instructions if necessary, and determine if the newly
225  // added instructions need to be processed as well.
226  //
227  while (Worklist.size())
228  {
229  Instruction* I = Worklist.back();
230  Worklist.pop_back();
231 
232  //
233  // Scan through the operands of this instruction and convert each into an
234  // instruction. Note that this works a little differently for phi
235  // instructions because the new instruction must be added to the
236  // appropriate predecessor block.
237  //
238  if (PHINode * PHI = SVFUtil::dyn_cast<PHINode>(I))
239  {
240  for (u32_t index = 0; index < PHI->getNumIncomingValues(); ++index)
241  {
242  //
243  // For PHI Nodes, if an operand is a constant expression with a GEP, we
244  // want to insert the new instructions in the predecessor basic block.
245  //
246  // Note: It seems that it's possible for a phi to have the same
247  // incoming basic block listed multiple times; this seems okay as long
248  // the same value is listed for the incoming block.
249  //
250  Instruction* InsertPt = PHI->getIncomingBlock(index)->getTerminator();
251  if (ConstantExpr * CE = hasConstantExpr(PHI->getIncomingValue(index)))
252  {
253  Instruction* NewInst = convertExpression (CE, InsertPt);
254  for (u32_t i2 = index; i2 < PHI->getNumIncomingValues(); ++i2)
255  {
256  if ((PHI->getIncomingBlock (i2)) == PHI->getIncomingBlock (index))
257  PHI->setIncomingValue (i2, NewInst);
258  }
259  Worklist.push_back (NewInst);
260  }
261  }
262  }
263  else
264  {
265  for (u32_t index = 0; index < I->getNumOperands(); ++index)
266  {
267  //
268  // For other instructions, we want to insert instructions replacing
269  // constant expressions immediately before the instruction using the
270  // constant expression.
271  //
272  if (ConstantExpr * CE = hasConstantExpr(I->getOperand(index)))
273  {
274  Instruction* NewInst = convertExpression (CE, I);
275  I->replaceUsesOfWith (CE, NewInst);
276  Worklist.push_back (NewInst);
277  }
278  }
279  }
280  }
281 
282  }
283  return modified;
284 }
static Instruction * convertExpression(ConstantExpr *CE, Instruction *InsertPt)
static ConstantExpr * hasConstantExpr(Value *V)
unsigned u32_t
Definition: CommandLine.h:18
#define F(f)
int index
Definition: cJSON.h:170
llvm::Instruction Instruction
Definition: BasicTypes.h:87
llvm::ConstantExpr ConstantExpr
Definition: BasicTypes.h:120
llvm::PHINode PHINode
Definition: BasicTypes.h:165

Member Data Documentation

◆ ID

char BreakConstantGEPs::ID = 0
static

Definition at line 38 of file BreakConstantExpr.h.


The documentation for this class was generated from the following files: