Deleted Added
full compact
Local.cpp (198090) Local.cpp (198892)
1//===-- Local.cpp - Functions to perform local transformations ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//

--- 45 unchanged lines hidden (view full) ---

54 // the load entirely).
55 BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
56
57 while (BBI != E) {
58 --BBI;
59
60 // If we see a free or a call which may write to memory (i.e. which might do
61 // a free) the pointer could be marked invalid.
1//===-- Local.cpp - Functions to perform local transformations ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//

--- 45 unchanged lines hidden (view full) ---

54 // the load entirely).
55 BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
56
57 while (BBI != E) {
58 --BBI;
59
60 // If we see a free or a call which may write to memory (i.e. which might do
61 // a free) the pointer could be marked invalid.
62 if (isa<FreeInst>(BBI) ||
63 (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
64 !isa<DbgInfoIntrinsic>(BBI)))
62 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
63 !isa<DbgInfoIntrinsic>(BBI))
65 return false;
66
67 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
68 if (LI->getOperand(0) == V) return true;
69 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
70 if (SI->getOperand(1) == V) return true;
71 }
72 }

--- 32 unchanged lines hidden (view full) ---

105 // it will adjust it's PHI nodes.
106 assert(BI->getParent() && "Terminator not inserted in block!");
107 OldDest->removePredecessor(BI->getParent());
108
109 // Set the unconditional destination, and change the insn to be an
110 // unconditional branch.
111 BI->setUnconditionalDest(Destination);
112 return true;
64 return false;
65
66 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
67 if (LI->getOperand(0) == V) return true;
68 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
69 if (SI->getOperand(1) == V) return true;
70 }
71 }

--- 32 unchanged lines hidden (view full) ---

104 // it will adjust it's PHI nodes.
105 assert(BI->getParent() && "Terminator not inserted in block!");
106 OldDest->removePredecessor(BI->getParent());
107
108 // Set the unconditional destination, and change the insn to be an
109 // unconditional branch.
110 BI->setUnconditionalDest(Destination);
111 return true;
113 } else if (Dest2 == Dest1) { // Conditional branch to same location?
112 }
113
114 if (Dest2 == Dest1) { // Conditional branch to same location?
114 // This branch matches something like this:
115 // br bool %cond, label %Dest, label %Dest
116 // and changes it into: br label %Dest
117
118 // Let the basic block know that we are letting go of one copy of it.
119 assert(BI->getParent() && "Terminator not inserted in block!");
120 Dest1->removePredecessor(BI->getParent());
121
122 // Change a conditional branch to unconditional.
123 BI->setUnconditionalDest(Dest1);
124 return true;
125 }
115 // This branch matches something like this:
116 // br bool %cond, label %Dest, label %Dest
117 // and changes it into: br label %Dest
118
119 // Let the basic block know that we are letting go of one copy of it.
120 assert(BI->getParent() && "Terminator not inserted in block!");
121 Dest1->removePredecessor(BI->getParent());
122
123 // Change a conditional branch to unconditional.
124 BI->setUnconditionalDest(Dest1);
125 return true;
126 }
126 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
127 return false;
128 }
129
130 if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
127 // If we are switching on a constant, we can convert the switch into a
128 // single branch instruction!
129 ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
130 BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest
131 BasicBlock *DefaultDest = TheOnlyDest;
132 assert(TheOnlyDest == SI->getDefaultDest() &&
133 "Default destination is not successor #0?");
134
131 // If we are switching on a constant, we can convert the switch into a
132 // single branch instruction!
133 ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
134 BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest
135 BasicBlock *DefaultDest = TheOnlyDest;
136 assert(TheOnlyDest == SI->getDefaultDest() &&
137 "Default destination is not successor #0?");
138
135 // Figure out which case it goes to...
139 // Figure out which case it goes to.
136 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
137 // Found case matching a constant operand?
138 if (SI->getSuccessorValue(i) == CI) {
139 TheOnlyDest = SI->getSuccessor(i);
140 break;
141 }
142
143 // Check to see if this branch is going to the same place as the default
144 // dest. If so, eliminate it as an explicit compare.
145 if (SI->getSuccessor(i) == DefaultDest) {
140 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
141 // Found case matching a constant operand?
142 if (SI->getSuccessorValue(i) == CI) {
143 TheOnlyDest = SI->getSuccessor(i);
144 break;
145 }
146
147 // Check to see if this branch is going to the same place as the default
148 // dest. If so, eliminate it as an explicit compare.
149 if (SI->getSuccessor(i) == DefaultDest) {
146 // Remove this entry...
150 // Remove this entry.
147 DefaultDest->removePredecessor(SI->getParent());
148 SI->removeCase(i);
149 --i; --e; // Don't skip an entry...
150 continue;
151 }
152
153 // Otherwise, check to see if the switch only branches to one destination.
154 // We do this by reseting "TheOnlyDest" to null when we find two non-equal

--- 5 unchanged lines hidden (view full) ---

160 // Branching on a constant, but not any of the cases, go to the default
161 // successor.
162 TheOnlyDest = SI->getDefaultDest();
163 }
164
165 // If we found a single destination that we can fold the switch into, do so
166 // now.
167 if (TheOnlyDest) {
151 DefaultDest->removePredecessor(SI->getParent());
152 SI->removeCase(i);
153 --i; --e; // Don't skip an entry...
154 continue;
155 }
156
157 // Otherwise, check to see if the switch only branches to one destination.
158 // We do this by reseting "TheOnlyDest" to null when we find two non-equal

--- 5 unchanged lines hidden (view full) ---

164 // Branching on a constant, but not any of the cases, go to the default
165 // successor.
166 TheOnlyDest = SI->getDefaultDest();
167 }
168
169 // If we found a single destination that we can fold the switch into, do so
170 // now.
171 if (TheOnlyDest) {
168 // Insert the new branch..
172 // Insert the new branch.
169 BranchInst::Create(TheOnlyDest, SI);
170 BasicBlock *BB = SI->getParent();
171
172 // Remove entries from PHI nodes which we no longer branch to...
173 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
174 // Found case matching a constant operand?
175 BasicBlock *Succ = SI->getSuccessor(i);
176 if (Succ == TheOnlyDest)
177 TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest
178 else
179 Succ->removePredecessor(BB);
180 }
181
173 BranchInst::Create(TheOnlyDest, SI);
174 BasicBlock *BB = SI->getParent();
175
176 // Remove entries from PHI nodes which we no longer branch to...
177 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
178 // Found case matching a constant operand?
179 BasicBlock *Succ = SI->getSuccessor(i);
180 if (Succ == TheOnlyDest)
181 TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest
182 else
183 Succ->removePredecessor(BB);
184 }
185
182 // Delete the old switch...
186 // Delete the old switch.
183 BB->getInstList().erase(SI);
184 return true;
187 BB->getInstList().erase(SI);
188 return true;
185 } else if (SI->getNumSuccessors() == 2) {
189 }
190
191 if (SI->getNumSuccessors() == 2) {
186 // Otherwise, we can fold this switch into a conditional branch
187 // instruction if it has only one non-default destination.
188 Value *Cond = new ICmpInst(SI, ICmpInst::ICMP_EQ, SI->getCondition(),
189 SI->getSuccessorValue(1), "cond");
192 // Otherwise, we can fold this switch into a conditional branch
193 // instruction if it has only one non-default destination.
194 Value *Cond = new ICmpInst(SI, ICmpInst::ICMP_EQ, SI->getCondition(),
195 SI->getSuccessorValue(1), "cond");
190 // Insert the new branch...
196 // Insert the new branch.
191 BranchInst::Create(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI);
192
197 BranchInst::Create(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI);
198
193 // Delete the old switch...
199 // Delete the old switch.
194 SI->eraseFromParent();
195 return true;
196 }
200 SI->eraseFromParent();
201 return true;
202 }
203 return false;
197 }
204 }
205
206 if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(T)) {
207 // indirectbr blockaddress(@F, @BB) -> br label @BB
208 if (BlockAddress *BA =
209 dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) {
210 BasicBlock *TheOnlyDest = BA->getBasicBlock();
211 // Insert the new branch.
212 BranchInst::Create(TheOnlyDest, IBI);
213
214 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
215 if (IBI->getDestination(i) == TheOnlyDest)
216 TheOnlyDest = 0;
217 else
218 IBI->getDestination(i)->removePredecessor(IBI->getParent());
219 }
220 IBI->eraseFromParent();
221
222 // If we didn't find our destination in the IBI successor list, then we
223 // have undefined behavior. Replace the unconditional branch with an
224 // 'unreachable' instruction.
225 if (TheOnlyDest) {
226 BB->getTerminator()->eraseFromParent();
227 new UnreachableInst(BB->getContext(), BB);
228 }
229
230 return true;
231 }
232 }
233
198 return false;
199}
200
201
202//===----------------------------------------------------------------------===//
203// Local dead code elimination...
204//
205

--- 145 unchanged lines hidden ---
234 return false;
235}
236
237
238//===----------------------------------------------------------------------===//
239// Local dead code elimination...
240//
241

--- 145 unchanged lines hidden ---