|
20 | 20 |
|
21 | 21 | #include "NVPTX.h"
|
22 | 22 | #include "llvm/ADT/SmallVector.h"
|
23 |
| -#include "llvm/Analysis/ConstantFolding.h" |
24 | 23 | #include "llvm/IR/Constants.h"
|
25 | 24 | #include "llvm/IR/DerivedTypes.h"
|
26 | 25 | #include "llvm/IR/Function.h"
|
|
37 | 36 | #include "llvm/Support/raw_os_ostream.h"
|
38 | 37 | #include "llvm/Support/raw_ostream.h"
|
39 | 38 | #include "llvm/Transforms/Scalar.h"
|
40 |
| -#include "llvm/Transforms/Utils/BasicBlockUtils.h" |
41 |
| -#include "llvm/Transforms/Utils/Local.h" |
42 | 39 | #include <sstream>
|
43 | 40 | #include <string>
|
44 | 41 | #define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
|
@@ -90,7 +87,6 @@ static bool runNVVMReflect(Function &F, unsigned SmVersion) {
|
90 | 87 | }
|
91 | 88 |
|
92 | 89 | SmallVector<Instruction *, 4> ToRemove;
|
93 |
| - SmallVector<ICmpInst *, 4> ToSimplify; |
94 | 90 |
|
95 | 91 | // Go through the calls in this function. Each call to __nvvm_reflect or
|
96 | 92 | // llvm.nvvm.reflect should be a CallInst with a ConstantArray argument.
|
@@ -175,71 +171,13 @@ static bool runNVVMReflect(Function &F, unsigned SmVersion) {
|
175 | 171 | } else if (ReflectArg == "__CUDA_ARCH") {
|
176 | 172 | ReflectVal = SmVersion * 10;
|
177 | 173 | }
|
178 |
| - |
179 |
| - // If the immediate user is a simple comparison we want to simplify it. |
180 |
| - // TODO: This currently does not handle switch instructions. |
181 |
| - for (User *U : Call->users()) |
182 |
| - if (ICmpInst *I = dyn_cast<ICmpInst>(U)) |
183 |
| - ToSimplify.push_back(I); |
184 |
| - |
185 | 174 | Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
|
186 | 175 | ToRemove.push_back(Call);
|
187 | 176 | }
|
188 | 177 |
|
189 | 178 | for (Instruction *I : ToRemove)
|
190 | 179 | I->eraseFromParent();
|
191 | 180 |
|
192 |
| - // The code guarded by __nvvm_reflect may be invalid for the target machine. |
193 |
| - // We need to do some basic dead code elimination to trim invalid code before |
194 |
| - // it reaches the backend at all optimization levels. |
195 |
| - SmallVector<BranchInst *> Simplified; |
196 |
| - for (ICmpInst *Cmp : ToSimplify) { |
197 |
| - Constant *LHS = dyn_cast<Constant>(Cmp->getOperand(0)); |
198 |
| - Constant *RHS = dyn_cast<Constant>(Cmp->getOperand(1)); |
199 |
| - |
200 |
| - if (!LHS || !RHS) |
201 |
| - continue; |
202 |
| - |
203 |
| - // If the comparison is a compile time constant we simply propagate it. |
204 |
| - Constant *C = ConstantFoldCompareInstOperands( |
205 |
| - Cmp->getPredicate(), LHS, RHS, Cmp->getModule()->getDataLayout()); |
206 |
| - |
207 |
| - if (!C) |
208 |
| - continue; |
209 |
| - |
210 |
| - for (User *U : Cmp->users()) |
211 |
| - if (BranchInst *I = dyn_cast<BranchInst>(U)) |
212 |
| - Simplified.push_back(I); |
213 |
| - |
214 |
| - Cmp->replaceAllUsesWith(C); |
215 |
| - Cmp->eraseFromParent(); |
216 |
| - } |
217 |
| - |
218 |
| - // Each instruction here is a conditional branch off of a constant true or |
219 |
| - // false value. Simply replace it with an unconditional branch to the |
220 |
| - // appropriate basic block and delete the rest if it is trivially dead. |
221 |
| - DenseSet<Instruction *> Removed; |
222 |
| - for (BranchInst *Branch : Simplified) { |
223 |
| - if (Removed.contains(Branch)) |
224 |
| - continue; |
225 |
| - |
226 |
| - ConstantInt *C = dyn_cast<ConstantInt>(Branch->getCondition()); |
227 |
| - if (!C || (!C->isOne() && !C->isZero())) |
228 |
| - continue; |
229 |
| - |
230 |
| - BasicBlock *TrueBB = |
231 |
| - C->isOne() ? Branch->getSuccessor(0) : Branch->getSuccessor(1); |
232 |
| - BasicBlock *FalseBB = |
233 |
| - C->isOne() ? Branch->getSuccessor(1) : Branch->getSuccessor(0); |
234 |
| - |
235 |
| - ReplaceInstWithInst(Branch, BranchInst::Create(TrueBB)); |
236 |
| - if (FalseBB->use_empty() && FalseBB->hasNPredecessors(0) && |
237 |
| - FalseBB->getFirstNonPHIOrDbg()) { |
238 |
| - Removed.insert(FalseBB->getFirstNonPHIOrDbg()); |
239 |
| - changeToUnreachable(FalseBB->getFirstNonPHIOrDbg()); |
240 |
| - } |
241 |
| - } |
242 |
| - |
243 | 181 | return ToRemove.size() > 0;
|
244 | 182 | }
|
245 | 183 |
|
|
0 commit comments