1 #include <magic/support/VariableRefs.h>
2 
3 using namespace llvm;
4 
5 namespace llvm {
6 
7 //===----------------------------------------------------------------------===//
8 // Constructors, destructor, and operators
9 //===----------------------------------------------------------------------===//
10 
11 VariableRefs::VariableRefs() {
12     clear();
13 }
14 
15 //===----------------------------------------------------------------------===//
16 // Getters
17 //===----------------------------------------------------------------------===//
18 
19 bool VariableRefs::isUnnecessaryInstruction(Instruction* inst) const {
20     //have already instruction in the entry block, skip
21     if(instructionInEntryBlock) {
22         return true;
23     }
24     //have already instruction in the same block, skip
25     if(instruction && inst->getParent() == instruction->getParent()) {
26         return true;
27     }
28 
29     return false;
30 }
31 
32 Instruction* VariableRefs::getInstruction() const {
33     return instruction;
34 }
35 
36 bool VariableRefs::isInstructionInEntryBlock() const {
37     return instructionInEntryBlock;
38 }
39 
40 //===----------------------------------------------------------------------===//
41 // Other public methods
42 //===----------------------------------------------------------------------===//
43 
44 void VariableRefs::addInstruction(Instruction* inst) {
45     //no instruction yet, update instruction
46     if(!instruction) {
47         instruction = inst;
48         return;
49     }
50     //have already instruction in another block, give up and resort to a single instruction in the entry block
51     setFunctionEntryInstruction(inst->getParent()->getParent());
52 }
53 
54 void VariableRefs::clear() {
55     instruction = NULL;
56     instructionInEntryBlock = false;
57 }
58 
59 //===----------------------------------------------------------------------===//
60 // Private methods
61 //===----------------------------------------------------------------------===//
62 
63 void VariableRefs::setFunctionEntryInstruction(Function* function) {
64     this->instruction = function->front().getFirstNonPHI();
65     this->instructionInEntryBlock = true;
66 }
67 
68 }
69