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 "llvm/IR/Function.h" | |
23 #include "llvm/IR/Instructions.h" | |
24 #include "llvm/Pass.h" | |
25 #include "llvm/Transforms/NaCl.h" | |
26 | |
27 using namespace llvm; | |
28 | |
29 namespace { | |
30 // This is a FunctionPass because our handling of PHI nodes means | |
31 // that our modifications may cross BasicBlocks. | |
32 struct ExpandConstantExpr : public FunctionPass { | |
33 static char ID; // Pass identification, replacement for typeid | |
34 ExpandConstantExpr() : FunctionPass(ID) { | |
35 initializeExpandConstantExprPass(*PassRegistry::getPassRegistry()); | |
36 } | |
37 | |
38 virtual bool runOnFunction(Function &Func); | |
39 }; | |
40 } | |
41 | |
42 char ExpandConstantExpr::ID = 0; | |
43 INITIALIZE_PASS(ExpandConstantExpr, "expand-constant-expr", | |
Derek Schuff
2013/03/14 21:23:50
if you use a static RegisterPass<ExpandConstantExp
Mark Seaborn
2013/03/14 21:44:18
But then what causes ExpandConstantExpr.cpp to get
| |
44 "Expand out ConstantExprs into Instructions", | |
45 false, false) | |
46 | |
47 static bool expandInstruction(Instruction *Inst) { | |
48 bool Modified = false; | |
49 for (unsigned OpNum = 0; OpNum < Inst->getNumOperands(); OpNum++) { | |
50 if (ConstantExpr *Expr = | |
51 dyn_cast<ConstantExpr>(Inst->getOperand(OpNum))) { | |
52 Modified = true; | |
53 Instruction *InsertPt = Inst; | |
54 if (PHINode *PN = dyn_cast<PHINode>(InsertPt)) { | |
55 // We cannot insert instructions before a PHI node, so insert | |
56 // before the incoming block's terminator. This could be | |
57 // suboptimal if the terminator is a conditional. | |
58 InsertPt = PN->getIncomingBlock(OpNum)->getTerminator(); | |
59 } | |
60 Instruction *NewInst = Expr->getAsInstruction(); | |
61 NewInst->insertBefore(InsertPt); | |
62 NewInst->setName("expanded"); | |
63 Inst->setOperand(OpNum, NewInst); | |
64 expandInstruction(NewInst); | |
65 } | |
66 } | |
67 return Modified; | |
68 } | |
69 | |
70 bool ExpandConstantExpr::runOnFunction(Function &Func) { | |
71 bool Modified = false; | |
72 for (llvm::Function::iterator BB = Func.begin(), E = Func.end(); | |
73 BB != E; | |
74 ++BB) { | |
75 for (BasicBlock::InstListType::iterator Inst = BB->begin(), E = BB->end(); | |
76 Inst != E; | |
77 ++Inst) { | |
78 Modified |= expandInstruction(Inst); | |
79 } | |
80 } | |
81 return Modified; | |
82 } | |
83 | |
84 FunctionPass *llvm::createExpandConstantExprPass() { | |
85 return new ExpandConstantExpr(); | |
86 } | |
OLD | NEW |