OLD | NEW |
(Empty) | |
| 1 //===- ExpandConstantExpr.cpp - Convert ConstantExprs to Instructions------===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This pass expands out ConstantExprs into Instructions. |
| 11 // |
| 12 // Note that this only converts ConstantExprs that are referenced by |
| 13 // Instructions. It does not convert ConstantExprs that are used as |
| 14 // initializers for global variables. |
| 15 // |
| 16 // This simplifies the language so that the PNaCl translator does not |
| 17 // need to handle ConstantExprs as part of a stable wire format for |
| 18 // PNaCl. |
| 19 // |
| 20 //===----------------------------------------------------------------------===// |
| 21 |
| 22 #include <map> |
| 23 |
| 24 #include "llvm/IR/Constants.h" |
| 25 #include "llvm/IR/Function.h" |
| 26 #include "llvm/IR/Instructions.h" |
| 27 #include "llvm/Pass.h" |
| 28 #include "llvm/Transforms/NaCl.h" |
| 29 |
| 30 using namespace llvm; |
| 31 |
| 32 static bool expandInstruction(Instruction *Inst); |
| 33 |
| 34 namespace { |
| 35 // This is a FunctionPass because our handling of PHI nodes means |
| 36 // that our modifications may cross BasicBlocks. |
| 37 struct ExpandConstantExpr : public FunctionPass { |
| 38 static char ID; // Pass identification, replacement for typeid |
| 39 ExpandConstantExpr() : FunctionPass(ID) { |
| 40 initializeExpandConstantExprPass(*PassRegistry::getPassRegistry()); |
| 41 } |
| 42 |
| 43 virtual bool runOnFunction(Function &Func); |
| 44 }; |
| 45 } |
| 46 |
| 47 char ExpandConstantExpr::ID = 0; |
| 48 INITIALIZE_PASS(ExpandConstantExpr, "expand-constant-expr", |
| 49 "Expand out ConstantExprs into Instructions", |
| 50 false, false) |
| 51 |
| 52 static Value *expandConstantExpr(Instruction *InsertPt, ConstantExpr *Expr) { |
| 53 Instruction *NewInst = Expr->getAsInstruction(); |
| 54 NewInst->insertBefore(InsertPt); |
| 55 NewInst->setName("expanded"); |
| 56 expandInstruction(NewInst); |
| 57 return NewInst; |
| 58 } |
| 59 |
| 60 static bool expandInstruction(Instruction *Inst) { |
| 61 // A landingpad can only accept ConstantExprs, so it should remain |
| 62 // unmodified. |
| 63 if (isa<LandingPadInst>(Inst)) |
| 64 return false; |
| 65 |
| 66 bool Modified = false; |
| 67 for (unsigned OpNum = 0; OpNum < Inst->getNumOperands(); OpNum++) { |
| 68 if (ConstantExpr *Expr = |
| 69 dyn_cast<ConstantExpr>(Inst->getOperand(OpNum))) { |
| 70 Modified = true; |
| 71 Use *U = &Inst->getOperandUse(OpNum); |
| 72 PhiSafeReplaceUses(U, expandConstantExpr(PhiSafeInsertPt(U), Expr)); |
| 73 } |
| 74 } |
| 75 return Modified; |
| 76 } |
| 77 |
| 78 bool ExpandConstantExpr::runOnFunction(Function &Func) { |
| 79 bool Modified = false; |
| 80 for (llvm::Function::iterator BB = Func.begin(), E = Func.end(); |
| 81 BB != E; |
| 82 ++BB) { |
| 83 for (BasicBlock::InstListType::iterator Inst = BB->begin(), E = BB->end(); |
| 84 Inst != E; |
| 85 ++Inst) { |
| 86 Modified |= expandInstruction(Inst); |
| 87 } |
| 88 } |
| 89 return Modified; |
| 90 } |
| 91 |
| 92 FunctionPass *llvm::createExpandConstantExprPass() { |
| 93 return new ExpandConstantExpr(); |
| 94 } |
OLD | NEW |