llvm.org GIT mirror llvm / dff6710
Integrate the readonly/readnone logic more deeply into alias analysis. This meant updating the API which now has versions of the getModRefBehavior, doesNotAccessMemory and onlyReadsMemory methods which take a callsite parameter. These should be used unless the callsite is not known, since in general they can do a better job than the versions that take a function. Also, users should no longer call the version of getModRefBehavior that takes both a function and a callsite. To reduce the chance of misuse it is now protected. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44487 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 13 years ago
12 changed file(s) with 160 addition(s) and 128 deletion(s). Raw diff Collapse all Expand all
185185 };
186186 };
187187
188 /// getModRefBehavior - Return the behavior when calling the given call site.
189 ModRefBehavior getModRefBehavior(CallSite CS,
190 std::vector *Info = 0);
191
192 /// getModRefBehavior - Return the behavior when calling the given function.
193 /// For use when the call site is not known.
194 ModRefBehavior getModRefBehavior(Function *F,
195 std::vector *Info = 0);
196
197 /// doesNotAccessMemory - If the specified call is known to never read or
198 /// write memory, return true. If the call only reads from known-constant
199 /// memory, it is also legal to return true. Calls that unwind the stack
200 /// are legal for this predicate.
201 ///
202 /// Many optimizations (such as CSE and LICM) can be performed on such calls
203 /// without worrying about aliasing properties, and many calls have this
204 /// property (e.g. calls to 'sin' and 'cos').
205 ///
206 /// This property corresponds to the GCC 'const' attribute.
207 ///
208 bool doesNotAccessMemory(CallSite CS) {
209 return getModRefBehavior(CS) == DoesNotAccessMemory;
210 }
211
212 /// doesNotAccessMemory - If the specified function is known to never read or
213 /// write memory, return true. For use when the call site is not known.
214 ///
215 bool doesNotAccessMemory(Function *F) {
216 return getModRefBehavior(F) == DoesNotAccessMemory;
217 }
218
219 /// onlyReadsMemory - If the specified call is known to only read from
220 /// non-volatile memory (or not access memory at all), return true. Calls
221 /// that unwind the stack are legal for this predicate.
222 ///
223 /// This property allows many common optimizations to be performed in the
224 /// absence of interfering store instructions, such as CSE of strlen calls.
225 ///
226 /// This property corresponds to the GCC 'pure' attribute.
227 ///
228 bool onlyReadsMemory(CallSite CS) {
229 ModRefBehavior MRB = getModRefBehavior(CS);
230 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
231 }
232
233 /// onlyReadsMemory - If the specified function is known to only read from
234 /// non-volatile memory (or not access memory at all), return true. For use
235 /// when the call site is not known.
236 ///
237 bool onlyReadsMemory(Function *F) {
238 ModRefBehavior MRB = getModRefBehavior(F);
239 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
240 }
241
242
243 /// getModRefInfo - Return information about whether or not an instruction may
244 /// read or write memory specified by the pointer operand. An instruction
245 /// that doesn't read or write memory may be trivially LICM'd for example.
246
247 /// getModRefInfo (for call sites) - Return whether information about whether
248 /// a particular call site modifies or reads the memory specified by the
249 /// pointer.
250 ///
251 virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
252
253 /// getModRefInfo - Return information about whether two call sites may refer
254 /// to the same set of memory locations. This function returns NoModRef if
255 /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory
256 /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or
257 /// ModRef if CS1 might read or write memory accessed by CS2.
258 ///
259 virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
260
261 /// hasNoModRefInfoForCalls - Return true if the analysis has no mod/ref
262 /// information for pairs of function calls (other than "pure" and "const"
263 /// functions). This can be used by clients to avoid many pointless queries.
264 /// Remember that if you override this and chain to another analysis, you must
265 /// make sure that it doesn't have mod/ref info either.
266 ///
267 virtual bool hasNoModRefInfoForCalls() const;
268
269 protected:
188270 /// getModRefBehavior - Return the behavior of the specified function if
189271 /// called from the specified call site. The call site may be null in which
190272 /// case the most generic behavior of this function should be returned.
191273 virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
192274 std::vector *Info = 0);
193275
194 /// doesNotAccessMemory - If the specified function is known to never read or
195 /// write memory, return true. If the function only reads from known-constant
196 /// memory, it is also legal to return true. Functions that unwind the stack
197 /// are not legal for this predicate.
198 ///
199 /// Many optimizations (such as CSE and LICM) can be performed on calls to it,
200 /// without worrying about aliasing properties, and many functions have this
201 /// property (e.g. 'sin' and 'cos').
202 ///
203 /// This property corresponds to the GCC 'const' attribute.
204 ///
205 bool doesNotAccessMemory(Function *F) {
206 return getModRefBehavior(F, CallSite()) == DoesNotAccessMemory;
207 }
208
209 /// onlyReadsMemory - If the specified function is known to only read from
210 /// non-volatile memory (or not access memory at all), return true. Functions
211 /// that unwind the stack are not legal for this predicate.
212 ///
213 /// This property allows many common optimizations to be performed in the
214 /// absence of interfering store instructions, such as CSE of strlen calls.
215 ///
216 /// This property corresponds to the GCC 'pure' attribute.
217 ///
218 bool onlyReadsMemory(Function *F) {
219 /// FIXME: If the analysis returns more precise info, we can reduce it to
220 /// this.
221 ModRefBehavior MRB = getModRefBehavior(F, CallSite());
222 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
223 }
224
225
226 /// getModRefInfo - Return information about whether or not an instruction may
227 /// read or write memory specified by the pointer operand. An instruction
228 /// that doesn't read or write memory may be trivially LICM'd for example.
229
230 /// getModRefInfo (for call sites) - Return whether information about whether
231 /// a particular call site modifies or reads the memory specified by the
232 /// pointer.
233 ///
234 virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
235
236 /// getModRefInfo - Return information about whether two call sites may refer
237 /// to the same set of memory locations. This function returns NoModRef if
238 /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory
239 /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or
240 /// ModRef if CS1 might read or write memory accessed by CS2.
241 ///
242 virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
243
244 /// hasNoModRefInfoForCalls - Return true if the analysis has no mod/ref
245 /// information for pairs of function calls (other than "pure" and "const"
246 /// functions). This can be used by clients to avoid many pointless queries.
247 /// Remember that if you override this and chain to another analysis, you must
248 /// make sure that it doesn't have mod/ref info either.
249 ///
250 virtual bool hasNoModRefInfoForCalls() const;
251
276 public:
252277 /// Convenience functions...
253278 ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
254279 ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size);
2626 #include "llvm/Analysis/AliasAnalysis.h"
2727 #include "llvm/Pass.h"
2828 #include "llvm/BasicBlock.h"
29 #include "llvm/Function.h"
2930 #include "llvm/Instructions.h"
3031 #include "llvm/Type.h"
3132 #include "llvm/Target/TargetData.h"
111112 return pointsToConstantMemory(P) ? NoModRef : Mod;
112113 }
113114
115 AliasAnalysis::ModRefBehavior
116 AliasAnalysis::getModRefBehavior(CallSite CS,
117 std::vector *Info) {
118 if (CS.paramHasAttr(0, ParamAttr::ReadNone))
119 // Can't do better than this.
120 return DoesNotAccessMemory;
121 ModRefBehavior MRB = UnknownModRefBehavior;
122 if (Function *F = CS.getCalledFunction())
123 MRB = getModRefBehavior(F, CS, Info);
124 if (MRB != DoesNotAccessMemory && CS.paramHasAttr(0, ParamAttr::ReadOnly))
125 return OnlyReadsMemory;
126 return MRB;
127 }
128
129 AliasAnalysis::ModRefBehavior
130 AliasAnalysis::getModRefBehavior(Function *F,
131 std::vector *Info) {
132 if (F->paramHasAttr(0, ParamAttr::ReadNone))
133 // Can't do better than this.
134 return DoesNotAccessMemory;
135 ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info);
136 if (MRB != DoesNotAccessMemory && F->paramHasAttr(0, ParamAttr::ReadOnly))
137 return OnlyReadsMemory;
138 return MRB;
139 }
140
114141 AliasAnalysis::ModRefResult
115142 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
116143 ModRefResult Mask = ModRef;
117 if (Function *F = CS.getCalledFunction()) {
118 ModRefBehavior MRB = getModRefBehavior(F, CallSite());
119 if (MRB == OnlyReadsMemory)
120 Mask = Ref;
121 else if (MRB == DoesNotAccessMemory)
122 return NoModRef;
123 }
144 ModRefBehavior MRB = getModRefBehavior(CS);
145 if (MRB == OnlyReadsMemory)
146 Mask = Ref;
147 else if (MRB == DoesNotAccessMemory)
148 return NoModRef;
124149
125150 if (!AA) return Mask;
126151
8888 bool pointsToConstantMemory(const Value *P) {
8989 return getAnalysis().pointsToConstantMemory(P);
9090 }
91 bool doesNotAccessMemory(CallSite CS) {
92 return getAnalysis().doesNotAccessMemory(CS);
93 }
9194 bool doesNotAccessMemory(Function *F) {
9295 return getAnalysis().doesNotAccessMemory(F);
96 }
97 bool onlyReadsMemory(CallSite CS) {
98 return getAnalysis().onlyReadsMemory(CS);
9399 }
94100 bool onlyReadsMemory(Function *F) {
95101 return getAnalysis().onlyReadsMemory(F);
113113 void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
114114 CallSites.push_back(CS);
115115
116 if (Function *F = CS.getCalledFunction()) {
117 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(F, CS);
118 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
119 return;
120 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
121 AliasTy = MayAlias;
122 AccessTy |= Refs;
123 return;
124 }
116 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
117 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
118 return;
119 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
120 AliasTy = MayAlias;
121 AccessTy |= Refs;
122 return;
125123 }
126124
127125 // FIXME: This should use mod/ref information to make this not suck so bad
165163 }
166164
167165 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
168 if (Function *F = CS.getCalledFunction())
169 if (AA.doesNotAccessMemory(F))
170 return false;
166 if (AA.doesNotAccessMemory(CS))
167 return false;
171168
172169 if (AA.hasNoModRefInfoForCalls())
173170 return true;
296293
297294
298295 bool AliasSetTracker::add(CallSite CS) {
299 if (Function *F = CS.getCalledFunction())
300 if (AA.doesNotAccessMemory(F))
301 return true; // doesn't alias anything
296 if (AA.doesNotAccessMemory(CS))
297 return true; // doesn't alias anything
302298
303299 AliasSet *AS = findAliasSetForCallSite(CS);
304300 if (!AS) {
418414 }
419415
420416 bool AliasSetTracker::remove(CallSite CS) {
421 if (Function *F = CS.getCalledFunction())
422 if (AA.doesNotAccessMemory(F))
423 return false; // doesn't alias anything
417 if (AA.doesNotAccessMemory(CS))
418 return false; // doesn't alias anything
424419
425420 AliasSet *AS = findAliasSetForCallSite(CS);
426421 if (!AS) return false;
454449 // If this is a call instruction, remove the callsite from the appropriate
455450 // AliasSet.
456451 CallSite CS = CallSite::get(PtrVal);
457 if (CS.getInstruction()) {
458 Function *F = CS.getCalledFunction();
459 if (!F || !AA.doesNotAccessMemory(F)) {
452 if (CS.getInstruction())
453 if (!AA.doesNotAccessMemory(CS))
460454 if (AliasSet *AS = findAliasSetForCallSite(CS))
461455 AS->removeCallSite(CS);
462 }
463 }
464456
465457 // First, look up the PointerRec for this pointer.
466458 hash_map::iterator I = PointerMap.find(PtrVal);
838838 return UnknownModRefBehavior;
839839 }
840840
841 if (F->paramHasAttr(0, ParamAttr::ReadNone))
842 return DoesNotAccessMemory;
843 if (F->paramHasAttr(0, ParamAttr::ReadOnly))
844 return OnlyReadsMemory;
845
846841 return UnknownModRefBehavior;
847842 }
848843
393393 // Okay, if we can't say anything about it, maybe some other alias
394394 // analysis can.
395395 ModRefBehavior MRB =
396 AliasAnalysis::getModRefBehavior(Callee, CallSite());
396 AliasAnalysis::getModRefBehavior(Callee);
397397 if (MRB != DoesNotAccessMemory) {
398398 // FIXME: could make this more aggressive for functions that just
399399 // read memory. We should just say they read all globals.
147147 Function *CF = CI->getCalledFunction();
148148 if (CF == 0) return; // Indirect call.
149149 AliasAnalysis &AA = getAnalysis();
150 AliasAnalysis::ModRefBehavior MRB = AA.getModRefBehavior(CF, CI);
150 AliasAnalysis::ModRefBehavior MRB = AA.getModRefBehavior(CI);
151151 if (MRB != AliasAnalysis::DoesNotAccessMemory &&
152152 MRB != AliasAnalysis::OnlyReadsMemory)
153153 return; // Nothing we can do for now.
226226 CantEqual = true;
227227 break;
228228 } else if (CallInst *CI = dyn_cast(I)) {
229 if (CI->getCalledFunction() == 0 ||
230 !AA.onlyReadsMemory(CI->getCalledFunction())) {
229 if (!AA.onlyReadsMemory(CI)) {
231230 CantEqual = true;
232231 break;
233232 }
9393
9494 // FreeInsts erase the entire structure
9595 pointerSize = ~0UL;
96 } else if (CallSite::get(QI).getInstruction() != 0 &&
97 cast(QI)->getCalledFunction()) {
96 } else if (isa(QI)) {
9897 AliasAnalysis::ModRefBehavior result =
99 AA.getModRefBehavior(cast(QI)->getCalledFunction(),
100 CallSite::get(QI));
98 AA.getModRefBehavior(CallSite::get(QI));
10199 if (result != AliasAnalysis::DoesNotAccessMemory &&
102100 result != AliasAnalysis::OnlyReadsMemory) {
103101 if (!start && !block) {
197197 for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) {
198198 Instruction *I = II++;
199199 if (CallInst *CI = dyn_cast(I)) {
200 Function *F = CI->getCalledFunction();
201 if (F && AA.onlyReadsMemory(F)) {
200 if (AA.onlyReadsMemory(CI)) {
202201 if (CI->use_empty()) {
203202 BB->getInstList().erase(CI);
204203 ++NumCallRemoved;
305305 // If this call does not access memory, it can't
306306 // be undeadifying any of our pointers.
307307 CallSite CS = CallSite::get(BBI);
308 if (CS.getCalledFunction() &&
309 AA.doesNotAccessMemory(CS.getCalledFunction()))
308 if (AA.doesNotAccessMemory(CS))
310309 continue;
311310
312311 unsigned modRef = 0;
340340
341341 uint32_t ValueTable::hash_operand(Value* v) {
342342 if (CallInst* CI = dyn_cast(v))
343 if (CI->getCalledFunction() &&
344 !AA->doesNotAccessMemory(CI->getCalledFunction()))
343 if (!AA->doesNotAccessMemory(CI))
345344 return nextValueNumber++;
346345
347346 return lookup_or_add(v);
484483 return VI->second;
485484
486485 if (CallInst* C = dyn_cast(V)) {
487 if (C->getCalledFunction() &&
488 (AA->doesNotAccessMemory(C->getCalledFunction()) ||
489 AA->onlyReadsMemory(C->getCalledFunction()))) {
486 if (AA->onlyReadsMemory(C)) { // includes doesNotAccessMemory
490487 Expression e = create_expression(C);
491488
492489 DenseMap::iterator EI = expressionNumbering.find(e);
10501047
10511048 if (CallInst* CI = dyn_cast(I)) {
10521049 AliasAnalysis& AA = getAnalysis();
1053 if (CI->getCalledFunction() &&
1054 !AA.doesNotAccessMemory(CI->getCalledFunction())) {
1050 if (!AA.doesNotAccessMemory(CI)) {
10551051 MemoryDependenceAnalysis& MD = getAnalysis();
10561052 if (cast(repl)->getParent() != CI->getParent() ||
10571053 MD.getDependency(CI) != MD.getDependency(cast(repl))) {
374374 return !pointerInvalidatedByLoop(LI->getOperand(0), Size);
375375 } else if (CallInst *CI = dyn_cast(&I)) {
376376 // Handle obvious cases efficiently.
377 if (Function *Callee = CI->getCalledFunction()) {
378 AliasAnalysis::ModRefBehavior Behavior =AA->getModRefBehavior(Callee, CI);
379 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
380 return true;
381 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
382 // If this call only reads from memory and there are no writes to memory
383 // in the loop, we can hoist or sink the call as appropriate.
384 bool FoundMod = false;
385 for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
386 I != E; ++I) {
387 AliasSet &AS = *I;
388 if (!AS.isForwardingAliasSet() && AS.isMod()) {
389 FoundMod = true;
390 break;
391 }
377 AliasAnalysis::ModRefBehavior Behavior = AA->getModRefBehavior(CI);
378 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
379 return true;
380 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
381 // If this call only reads from memory and there are no writes to memory
382 // in the loop, we can hoist or sink the call as appropriate.
383 bool FoundMod = false;
384 for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
385 I != E; ++I) {
386 AliasSet &AS = *I;
387 if (!AS.isForwardingAliasSet() && AS.isMod()) {
388 FoundMod = true;
389 break;
392390 }
393 if (!FoundMod) return true;
394391 }
392 if (!FoundMod) return true;
395393 }
396394
397395 // FIXME: This should use mod/ref information to see if we can hoist or sink