OpenASIP  2.0
LLVMAliasAnalyzer.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2020 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file LLVMAliasAnalyzer.cc
26  *
27  * Implementation of LLVMAliasAnalyzer class.
28  *
29  * This class uses LLVM AliasAnalysis and MachineInstruction references
30  * in ProgramOperation to query for alises.
31  *
32  * @author VladimĂ­r Guzma 2011 (vladimir.guzma-no.spam-tut.fi)
33  * @note rating: red
34  */
35 
36 #include "CompilerWarnings.hh"
37 IGNORE_COMPILER_WARNING("-Wunused-parameter")
38 
39 #include "LLVMAliasAnalyzer.hh"
40 
41 #include "CompilerWarnings.hh"
42 IGNORE_COMPILER_WARNING("-Wunused-parameter")
43 IGNORE_COMPILER_WARNING("-Wcomment")
44 
45 #include <llvm/CodeGen/MachineInstr.h>
46 #include "tce_config.h"
47 #include <llvm/IR/Value.h>
48 #include <llvm/CodeGen/MachineMemOperand.h>
49 
50 #include <llvm/Analysis/AliasAnalysis.h>
51 
52 
53 #include "MoveNode.hh"
54 #include "Move.hh"
55 #include "DataDependenceGraph.hh"
56 #include "Terminal.hh"
57 
59 
60 using namespace TTAProgram;
61 using namespace TTAMachine;
62 
63 #define MayAlias llvm::AliasResult::Kind::MayAlias
64 #define PartialAlias llvm::AliasResult::Kind::PartialAlias
65 #define MustAlias llvm::AliasResult::Kind::MustAlias
66 #define NoAlias llvm::AliasResult::Kind::NoAlias
67 
69  AA_ = NULL;
70 }
71 
72 bool
74  DataDependenceGraph& /*ddg*/,
75  const ProgramOperation& po) {
76  // If the ProgramOperation has access to it's originating LLVM MachineInstr
77  // and MachineInstr have some memory operands we can try tracing memory
78  // accessed.
79  const llvm::MachineInstr* instr = po.machineInstr();
80  if (instr && !instr->memoperands_empty()) {
81  return true;
82  } else {
83  return false;
84  }
85 }
86 
87 /**
88  * Given two program operation, aswer question if memory accesed by those
89  * operations aliases.
90  *
91  * If both POs access same memory returns true, if is sure the memory
92  * is not same, returns false. Returns unknown in case memory accesses
93  * may alias, or if there is partial alias.
94  */
97  DataDependenceGraph& ddg,
98  const ProgramOperation& pop1,
99  const ProgramOperation& pop2,
100  MoveNodeUse::BBRelation bbInfo) {
101 
102  if (bbInfo) {
103  return ALIAS_UNKNOWN;
104  }
105 
106  if (!isAddressTraceable(ddg, pop1) ||
107  !isAddressTraceable(ddg, pop2) ||
108  AA_ == NULL) {
109  return ALIAS_UNKNOWN;
110  }
111 
112  const llvm::MachineInstr* instr1 = pop1.machineInstr();
113  const llvm::MachineInstr* instr2 = pop2.machineInstr();
114 
115  llvm::MachineInstr::mmo_iterator begin1 =
116  instr1->memoperands_begin();
117  // Machine instruction could in theory have several memory operands.
118  // In practice it is usually just one.
119  MemoryAliasAnalyzer::AliasingResult result = ALIAS_UNKNOWN;
120  while (begin1 != instr1->memoperands_end()) {
121  const llvm::Value* val1 = (*begin1)->getValue();
122  uint64_t size1 = (*begin1)->getSize();
123  llvm::MachineInstr::mmo_iterator begin2 =
124  instr2->memoperands_begin();
125 
126  while (begin2 != instr2->memoperands_end()) {
127  const llvm::Value* val2 = (*begin2)->getValue();
128  uint64_t size2 = (*begin2)->getSize();
129  if (val1 && val2) {
130 
131  AliasResult res =
132  AA_->alias(val1, size1, val2, size2);
133 
134  if (res == MayAlias || res == PartialAlias) {
135  result = ALIAS_UNKNOWN;
136  }
137  if (res == MustAlias) {
138  result = ALIAS_TRUE;
139  }
140  if (res == NoAlias) {
141  result = ALIAS_FALSE;
142  }
143  } else {
144  result = ALIAS_UNKNOWN;
145  }
146  begin2++;
147  }
148  begin1++;
149  }
150  return result;
151 }
152 
153 /**
154  * Sets active Alias Analyzer picked from LLVM.
155  */
156 void
158  AA_ = AA;
159 }
160 
MayAlias
#define MayAlias
Definition: LLVMAliasAnalyzer.cc:63
TTAProgram
Definition: Estimator.hh:65
AliasResult
llvm::AliasResult AliasResult
Definition: LLVMAliasAnalyzer.hh:52
MoveNodeUse::BBRelation
BBRelation
Definition: MoveNodeUse.hh:23
MustAlias
#define MustAlias
Definition: LLVMAliasAnalyzer.cc:65
LLVMAliasAnalyzer::analyze
virtual AliasingResult analyze(DataDependenceGraph &ddg, const ProgramOperation &pop1, const ProgramOperation &pop2, MoveNodeUse::BBRelation bbInfo)
Definition: LLVMAliasAnalyzer.cc:96
LLVMAliasAnalyzer::isAddressTraceable
virtual bool isAddressTraceable(DataDependenceGraph &ddg, const ProgramOperation &pop)
Definition: LLVMAliasAnalyzer.cc:73
DataDependenceGraph.hh
ProgramOperation
Definition: ProgramOperation.hh:70
ProgramOperation::machineInstr
const llvm::MachineInstr * machineInstr() const
Definition: ProgramOperation.hh:136
Terminal.hh
LLVMAliasAnalyzer::~LLVMAliasAnalyzer
~LLVMAliasAnalyzer()
Definition: LLVMAliasAnalyzer.cc:161
MemoryAliasAnalyzer::AliasingResult
AliasingResult
Definition: MemoryAliasAnalyzer.hh:50
NoAlias
#define NoAlias
Definition: LLVMAliasAnalyzer.cc:66
LLVMAliasAnalyzer::LLVMAliasAnalyzer
LLVMAliasAnalyzer()
Definition: LLVMAliasAnalyzer.cc:68
PartialAlias
#define PartialAlias
Definition: LLVMAliasAnalyzer.cc:64
IGNORE_COMPILER_WARNING
#define IGNORE_COMPILER_WARNING(X)
Definition: CompilerWarnings.hh:51
DataDependenceGraph
Definition: DataDependenceGraph.hh:67
POP_COMPILER_DIAGS
#define POP_COMPILER_DIAGS
Definition: CompilerWarnings.hh:68
LLVMAliasAnalyzer::setLLVMAA
virtual void setLLVMAA(llvm::AliasAnalysis *AA)
Definition: LLVMAliasAnalyzer.cc:157
Move.hh
TTAMachine
Definition: Assembler.hh:48
MoveNode.hh
llvm::AliasAnalysis
AAResults AliasAnalysis
Definition: DataDependenceGraphBuilder.hh:45
CompilerWarnings.hh