-
Notifications
You must be signed in to change notification settings - Fork 10
/
expand_constantexpr.cc
152 lines (135 loc) · 4.99 KB
/
expand_constantexpr.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//===- expand_constantexpr.cc - A pass for simplifying LLVM IR-------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "expand_constantexpr.h"
#include <llvm/BasicBlock.h>
#include <llvm/Constants.h>
#include <llvm/Function.h>
#include <llvm/InstrTypes.h>
#include <llvm/Instructions.h>
#include <llvm/Module.h>
#include <llvm/Operator.h>
#include <llvm/Pass.h>
using namespace llvm;
static Value *expandConstantExpr(Value *Val, Instruction *InsertPt);
namespace {
// This is a FunctionPass because our handling of PHI nodes means
// that our modifications may cross BasicBlocks.
class ExpandConstantExpr : public FunctionPass {
public:
static char ID; // Pass identification, replacement for typeid
ExpandConstantExpr() : FunctionPass(ID) {
}
virtual bool runOnFunction(Function &func);
};
}
char ExpandConstantExpr::ID = 0;
// Based on ConstantExpr::getAsInstruction() in lib/VMCore/Constants.cpp.
// TODO: Use getAsInstruction() when we require a newer LLVM version.
static Instruction *getConstantExprAsInstruction(ConstantExpr *CE,
Instruction *InsertPt) {
SmallVector<Value*,4> ValueOperands;
for (ConstantExpr::op_iterator I = CE->op_begin(), E = CE->op_end();
I != E;
++I)
ValueOperands.push_back(expandConstantExpr(cast<Value>(I), InsertPt));
ArrayRef<Value*> Ops(ValueOperands);
switch (CE->getOpcode()) {
case Instruction::Trunc:
case Instruction::ZExt:
case Instruction::SExt:
case Instruction::FPTrunc:
case Instruction::FPExt:
case Instruction::UIToFP:
case Instruction::SIToFP:
case Instruction::FPToUI:
case Instruction::FPToSI:
case Instruction::PtrToInt:
case Instruction::IntToPtr:
case Instruction::BitCast:
return CastInst::Create((Instruction::CastOps)CE->getOpcode(),
Ops[0], CE->getType());
case Instruction::Select:
return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
case Instruction::InsertElement:
return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
case Instruction::ExtractElement:
return ExtractElementInst::Create(Ops[0], Ops[1]);
case Instruction::InsertValue:
return InsertValueInst::Create(Ops[0], Ops[1], CE->getIndices());
case Instruction::ExtractValue:
return ExtractValueInst::Create(Ops[0], CE->getIndices());
case Instruction::ShuffleVector:
return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]);
case Instruction::GetElementPtr:
if (cast<GEPOperator>(CE)->isInBounds())
return GetElementPtrInst::CreateInBounds(Ops[0], Ops.slice(1));
else
return GetElementPtrInst::Create(Ops[0], Ops.slice(1));
case Instruction::ICmp:
case Instruction::FCmp:
return CmpInst::Create((Instruction::OtherOps)CE->getOpcode(),
CE->getPredicate(), Ops[0], Ops[1]);
default:
assert(CE->getNumOperands() == 2 && "Must be binary operator?");
BinaryOperator *BO =
BinaryOperator::Create((Instruction::BinaryOps)CE->getOpcode(),
Ops[0], Ops[1]);
if (OverflowingBinaryOperator *Op =
dyn_cast<OverflowingBinaryOperator>(BO)) {
BO->setHasNoUnsignedWrap(Op->hasNoUnsignedWrap());
BO->setHasNoSignedWrap(Op->hasNoSignedWrap());
}
if (PossiblyExactOperator *Op = dyn_cast<PossiblyExactOperator>(BO))
BO->setIsExact(Op->isExact());
return BO;
}
}
static Value *expandConstantExpr(Value *Val, Instruction *InsertPt) {
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Val)) {
Instruction *NewInst = getConstantExprAsInstruction(CE, InsertPt);
NewInst->insertBefore(InsertPt);
NewInst->setName("expanded");
return NewInst;
}
return Val;
}
static bool expandInstruction(Instruction *inst) {
bool modified = false;
for (unsigned opnum = 0; opnum < inst->getNumOperands(); opnum++) {
if (ConstantExpr *expr =
dyn_cast<ConstantExpr>(inst->getOperand(opnum))) {
modified = true;
Instruction *insert_pt = inst;
if (PHINode *pn = dyn_cast<PHINode>(insert_pt)) {
// We cannot insert instructions before a PHI node, so insert
// before the incoming block's terminator. This could be
// suboptimal if the terminator is a conditional.
insert_pt = pn->getIncomingBlock(opnum)->getTerminator();
}
inst->setOperand(opnum, expandConstantExpr(expr, insert_pt));
}
}
return modified;
}
bool ExpandConstantExpr::runOnFunction(Function &func) {
bool modified = false;
for (llvm::Function::iterator bb = func.begin();
bb != func.end();
++bb) {
for (BasicBlock::InstListType::iterator inst = bb->begin();
inst != bb->end();
++inst) {
modified |= expandInstruction(inst);
}
}
return modified;
}
FunctionPass *createExpandConstantExprPass() {
return new ExpandConstantExpr();
}