llvm.org GIT mirror llvm / 7af1c78
Allow readonly functions to unwind exceptions. Teach the optimizers about this. For example, a readonly function with no uses cannot be removed unless it is also marked nounwind. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71071 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 10 years ago
13 changed file(s) with 54 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
10601060 behavior is undefined.
10611061
10621062
readnone
1063
This attribute indicates that the function computes its result (or the
1064 exception it throws) based strictly on its arguments, without dereferencing any
1063
This attribute indicates that the function computes its result (or decides to
1064 unwind an exception) based strictly on its arguments, without dereferencing any
10651065 pointer arguments or otherwise accessing any mutable state (e.g. memory, control
10661066 registers, etc) visible to caller functions. It does not write through any
10671067 pointer arguments (including byval arguments) and
1068 never changes any state visible to callers. readnone functions may not throw
1069 an exception that escapes into the caller.
1068 never changes any state visible to callers. This means that it cannot unwind
1069 exceptions by calling the C++ exception throwing methods, but could
1070 use the unwind instruction.
10701071
10711072
readonly
10721073
This attribute indicates that the function does not write through any
10731074 pointer arguments (including byval arguments)
10741075 or otherwise modify any state (e.g. memory, control registers, etc) visible to
10751076 caller functions. It may dereference pointer arguments and read state that may
1076 be set in the caller. A readonly function always returns the same value when
1077 called with the same set of arguments and global
1078 state. readonly functions may not throw an exception that escapes into the
1079 caller.
1077 be set in the caller. A readonly function always returns the same value (or
1078 unwinds an exception identically) when called with the same set of arguments
1079 and global state. It cannot unwind an exception by calling the C++
1080 exception throwing methods, but may use the unwind instruction.
10801081
10811082
ssp
10821083
This attribute indicates that the function should emit a stack smashing
3939 // Out of line virtual method, so the vtable, etc has a home.
4040 ~Instruction();
4141
42 /// mayWriteToMemory - Return true if this instruction may modify memory.
43 ///
44 bool mayWriteToMemory() const;
45
46 /// mayReadFromMemory - Return true if this instruction may read memory.
47 ///
48 bool mayReadFromMemory() const;
49
5042 /// clone() - Create a copy of 'this' instruction that is identical in all
5143 /// ways except the following:
5244 /// * The instruction has no parent
180172 }
181173 static bool isTrapping(unsigned op);
182174
175 /// mayWriteToMemory - Return true if this instruction may modify memory.
176 ///
177 bool mayWriteToMemory() const;
178
179 /// mayReadFromMemory - Return true if this instruction may read memory.
180 ///
181 bool mayReadFromMemory() const;
182
183 /// mayThrow - Return true if this instruction may throw an exception.
184 ///
185 bool mayThrow() const;
186
187 /// mayHaveSideEffects - Return true if the instruction may have side effects.
188 ///
189 bool mayHaveSideEffects() const {
190 return mayWriteToMemory() || mayThrow();
191 }
192
183193 /// Methods for support type inquiry through isa, cast, and dyn_cast:
184194 static inline bool classof(const Instruction *) { return true; }
185195 static inline bool classof(const Value *V) {
5656 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
5757 if (isa(I.getInstructionIterator()) ||
5858 isa(I.getInstructionIterator()) ||
59 I->mayWriteToMemory()) {
59 I->mayHaveSideEffects()) {
6060 alive.insert(I.getInstructionIterator());
6161 worklist.push_back(I.getInstructionIterator());
6262 }
14781478 for (BasicBlock::iterator BI = CurrentBlock->begin(),
14791479 BE = CurrentBlock->end(); BI != BE; ) {
14801480 Instruction *CurInst = BI++;
1481
1481
14821482 if (isa(CurInst) || isa(CurInst) ||
14831483 isa(CurInst) || (CurInst->getType() == Type::VoidTy) ||
1484 CurInst->mayReadFromMemory() || CurInst->mayWriteToMemory() ||
1484 CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
14851485 isa(CurInst))
14861486 continue;
1487
1487
14881488 uint32_t valno = VN.lookup(CurInst);
14891489
14901490 // Look for the predecessors for PRE opportunities. We're
1257812578 assert(I->hasOneUse() && "Invariants didn't hold!");
1257912579
1258012580 // Cannot move control-flow-involving, volatile loads, vaarg, etc.
12581 if (isa(I) || I->mayWriteToMemory() || isa(I))
12581 if (isa(I) || I->mayHaveSideEffects() || isa(I))
1258212582 return false;
1258312583
1258412584 // Do not sink alloca instructions out of the entry block.
135135 LI != LE; ++LI) {
136136 for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end();
137137 BI != BE; ++BI) {
138 if (BI->mayWriteToMemory())
138 if (BI->mayHaveSideEffects())
139139 return false;
140 else if (LoadInst* L = dyn_cast(BI))
141 if (L->isVolatile())
142 return false;
143140 }
144141 }
145142
11471147 || isa(I))
11481148 continue;
11491149
1150 if (I->mayWriteToMemory())
1150 if (I->mayHaveSideEffects())
11511151 return false;
11521152
11531153 // I is used only inside this block then it is OK.
299299 // Okay, everything after this looks good, check to make sure that this block
300300 // doesn't include any side effects.
301301 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
302 if (I->mayWriteToMemory())
302 if (I->mayHaveSideEffects())
303303 return false;
304304
305305 return true;
382382 // part of the loop that the code *would* execute. We already checked the
383383 // tail, check the header now.
384384 for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I)
385 if (I->mayWriteToMemory())
385 if (I->mayHaveSideEffects())
386386 return false;
387387 return true;
388388 }
257257 while (!isa(BBI)) {
258258 Instruction *I = BBI++;
259259
260 bool CanHoist = !I->isTrapping() && !I->mayWriteToMemory();
260 bool CanHoist = !I->isTrapping() && !I->mayHaveSideEffects();
261261 if (CanHoist) {
262262 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
263263 if (Instruction *OpI = dyn_cast(I->getOperand(op)))
200200 bool TailCallElim::CanMoveAboveCall(Instruction *I, CallInst *CI) {
201201 // FIXME: We can move load/store/call/free instructions above the call if the
202202 // call does not mod/ref the memory location being processed.
203 if (I->mayWriteToMemory() || isa(I))
203 if (I->mayHaveSideEffects() || isa(I))
204204 return false;
205205
206206 // Otherwise, if this is a side-effect free instruction, check to make sure
163163
164164 // We don't want debug info removed by anything this general.
165165 if (isa(I)) return false;
166
167 if (!I->mayWriteToMemory())
168 return true;
169
170 // Special case intrinsics that "may write to memory" but can be deleted when
171 // dead.
166
167 if (!I->mayHaveSideEffects()) return true;
168
169 // Special case intrinsics that "may have side effects" but can be deleted
170 // when dead.
172171 if (IntrinsicInst *II = dyn_cast(I))
173172 // Safe to delete llvm.stacksave if dead.
174173 if (II->getIntrinsicID() == Intrinsic::stacksave)
175174 return true;
176
177175 return false;
178176 }
179177
229227 SmallPtrSet PHIs;
230228 PHIs.insert(PN);
231229 for (Instruction *J = cast(*PN->use_begin());
232 J->hasOneUse() && !J->mayWriteToMemory();
230 J->hasOneUse() && !J->mayHaveSideEffects();
233231 J = cast(*J->use_begin()))
234232 // If we find a PHI more than once, we're on a cycle that
235233 // won't prove fruitful.
319319 }
320320 }
321321
322 /// mayThrow - Return true if this instruction may throw an exception.
323 ///
324 bool Instruction::mayThrow() const {
325 if (const CallInst *CI = dyn_cast(this))
326 return !CI->doesNotThrow();
327 return false;
328 }
329
322330 /// isAssociative - Return true if the instruction is associative:
323331 ///
324332 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
0 ; RUN: llvm-as < %s | opt -adce | llvm-dis | not grep call
11
2 declare i32 @strlen(i8*) readonly
2 declare i32 @strlen(i8*) readonly nounwind
33
44 define void @test() {
5 call i32 @strlen( i8* null ) readonly ; :1 [#uses=0]
5 call i32 @strlen( i8* null ) ; :1 [#uses=0]
66 ret void
77 }