clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SCCPSolver.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Transforms/Utils/SCCPSolver.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | #include "llvm/Transforms/Utils/SCCPSolver.h" |
| 16 | #include "llvm/Analysis/ConstantFolding.h" |
| 17 | #include "llvm/Analysis/InstructionSimplify.h" |
| 18 | #include "llvm/Analysis/ValueTracking.h" |
| 19 | #include "llvm/InitializePasses.h" |
| 20 | #include "llvm/Pass.h" |
| 21 | #include "llvm/Support/Casting.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/Transforms/Utils/Local.h" |
| 26 | #include <cassert> |
| 27 | #include <utility> |
| 28 | #include <vector> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | #define DEBUG_TYPE "sccp" |
| 33 | |
| 34 | |
| 35 | |
| 36 | static const unsigned MaxNumRangeExtensions = 10; |
| 37 | |
| 38 | |
| 39 | static ValueLatticeElement::MergeOptions getMaxWidenStepsOpts() { |
| 40 | return ValueLatticeElement::MergeOptions().setMaxWidenSteps( |
| 41 | MaxNumRangeExtensions); |
| 42 | } |
| 43 | |
| 44 | namespace { |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | bool isConstant(const ValueLatticeElement &LV) { |
| 51 | return LV.isConstant() || |
| 52 | (LV.isConstantRange() && LV.getConstantRange().isSingleElement()); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | bool isOverdefined(const ValueLatticeElement &LV) { |
| 60 | return !LV.isUnknownOrUndef() && !isConstant(LV); |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |
| 65 | namespace llvm { |
| 66 | |
| 67 | |
| 68 | |
| 69 | class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> { |
| 70 | const DataLayout &DL; |
| 71 | std::function<const TargetLibraryInfo &(Function &)> GetTLI; |
| 72 | SmallPtrSet<BasicBlock *, 8> BBExecutable; |
| 73 | DenseMap<Value *, ValueLatticeElement> |
| 74 | ValueState; |
| 75 | |
| 76 | |
| 77 | |
| 78 | DenseMap<std::pair<Value *, unsigned>, ValueLatticeElement> StructValueState; |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | DenseMap<GlobalVariable *, ValueLatticeElement> TrackedGlobals; |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | MapVector<Function *, ValueLatticeElement> TrackedRetVals; |
| 90 | |
| 91 | |
| 92 | |
| 93 | MapVector<std::pair<Function *, unsigned>, ValueLatticeElement> |
| 94 | TrackedMultipleRetVals; |
| 95 | |
| 96 | |
| 97 | |
| 98 | SmallPtrSet<Function *, 16> MRVFunctionsTracked; |
| 99 | |
| 100 | |
| 101 | SmallPtrSet<Function *, 16> MustPreserveReturnsInFunctions; |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | SmallPtrSet<Function *, 16> TrackingIncomingArguments; |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | SmallVector<Value *, 64> OverdefinedInstWorkList; |
| 116 | SmallVector<Value *, 64> InstWorkList; |
| 117 | |
| 118 | |
| 119 | SmallVector<BasicBlock *, 64> BBWorkList; |
| 120 | |
| 121 | |
| 122 | |
| 123 | using Edge = std::pair<BasicBlock *, BasicBlock *>; |
| 124 | DenseSet<Edge> KnownFeasibleEdges; |
| 125 | |
| 126 | DenseMap<Function *, AnalysisResultsForFn> AnalysisResults; |
| 127 | DenseMap<Value *, SmallPtrSet<User *, 2>> AdditionalUsers; |
| 128 | |
| 129 | LLVMContext &Ctx; |
| 130 | |
| 131 | private: |
| 132 | ConstantInt *getConstantInt(const ValueLatticeElement &IV) const { |
| 133 | return dyn_cast_or_null<ConstantInt>(getConstant(IV)); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | void pushToWorkList(ValueLatticeElement &IV, Value *V); |
| 138 | |
| 139 | |
| 140 | |
| 141 | void pushToWorkListMsg(ValueLatticeElement &IV, Value *V); |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | bool markConstant(ValueLatticeElement &IV, Value *V, Constant *C, |
| 147 | bool MayIncludeUndef = false); |
| 148 | |
| 149 | bool markConstant(Value *V, Constant *C) { |
| 150 | assert(!V->getType()->isStructTy() && "structs should use mergeInValue"); |
| 151 | return markConstant(ValueState[V], V, C); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | bool markOverdefined(ValueLatticeElement &IV, Value *V); |
| 158 | |
| 159 | |
| 160 | |
| 161 | bool mergeInValue(ValueLatticeElement &IV, Value *V, |
| 162 | ValueLatticeElement MergeWithV, |
| 163 | ValueLatticeElement::MergeOptions Opts = { |
| 164 | false, false}); |
| 165 | |
| 166 | bool mergeInValue(Value *V, ValueLatticeElement MergeWithV, |
| 167 | ValueLatticeElement::MergeOptions Opts = { |
| 168 | false, false}) { |
| 169 | assert(!V->getType()->isStructTy() && |
| 170 | "non-structs should use markConstant"); |
| 171 | return mergeInValue(ValueState[V], V, MergeWithV, Opts); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | ValueLatticeElement &getValueState(Value *V) { |
| 178 | assert(!V->getType()->isStructTy() && "Should use getStructValueState"); |
| 179 | |
| 180 | auto I = ValueState.insert(std::make_pair(V, ValueLatticeElement())); |
| 181 | ValueLatticeElement &LV = I.first->second; |
| 182 | |
| 183 | if (!I.second) |
| 184 | return LV; |
| 185 | |
| 186 | if (auto *C = dyn_cast<Constant>(V)) |
| 187 | LV.markConstant(C); |
| 188 | |
| 189 | |
| 190 | return LV; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | ValueLatticeElement &getStructValueState(Value *V, unsigned i) { |
| 197 | assert(V->getType()->isStructTy() && "Should use getValueState"); |
| 198 | assert(i < cast<StructType>(V->getType())->getNumElements() && |
| 199 | "Invalid element #"); |
| 200 | |
| 201 | auto I = StructValueState.insert( |
| 202 | std::make_pair(std::make_pair(V, i), ValueLatticeElement())); |
| 203 | ValueLatticeElement &LV = I.first->second; |
| 204 | |
| 205 | if (!I.second) |
| 206 | return LV; |
| 207 | |
| 208 | if (auto *C = dyn_cast<Constant>(V)) { |
| 209 | Constant *Elt = C->getAggregateElement(i); |
| 210 | |
| 211 | if (!Elt) |
| 212 | LV.markOverdefined(); |
| 213 | else if (isa<UndefValue>(Elt)) |
| 214 | ; |
| 215 | else |
| 216 | LV.markConstant(Elt); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | return LV; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | bool markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest); |
| 226 | |
| 227 | |
| 228 | |
| 229 | void getFeasibleSuccessors(Instruction &TI, SmallVectorImpl<bool> &Succs); |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | void operandChangedState(Instruction *I) { |
| 235 | if (BBExecutable.count(I->getParent())) |
| 236 | visit(*I); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | void addAdditionalUser(Value *V, User *U) { |
| 241 | auto Iter = AdditionalUsers.insert({V, {}}); |
| 242 | Iter.first->second.insert(U); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | void markUsersAsChanged(Value *I) { |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | if (isa<Function>(I)) { |
| 252 | for (User *U : I->users()) { |
| 253 | if (auto *CB = dyn_cast<CallBase>(U)) |
| 254 | handleCallResult(*CB); |
| 255 | } |
| 256 | } else { |
| 257 | for (User *U : I->users()) |
| 258 | if (auto *UI = dyn_cast<Instruction>(U)) |
| 259 | operandChangedState(UI); |
| 260 | } |
| 261 | |
| 262 | auto Iter = AdditionalUsers.find(I); |
| 263 | if (Iter != AdditionalUsers.end()) { |
| 264 | |
| 265 | |
| 266 | SmallVector<Instruction *, 2> ToNotify; |
| 267 | for (User *U : Iter->second) |
| 268 | if (auto *UI = dyn_cast<Instruction>(U)) |
| 269 | ToNotify.push_back(UI); |
| 270 | for (Instruction *UI : ToNotify) |
| 271 | operandChangedState(UI); |
| 272 | } |
| 273 | } |
| 274 | void handleCallOverdefined(CallBase &CB); |
| 275 | void handleCallResult(CallBase &CB); |
| 276 | void handleCallArguments(CallBase &CB); |
| 277 | |
| 278 | private: |
| 279 | friend class InstVisitor<SCCPInstVisitor>; |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | void visitPHINode(PHINode &I); |
| 285 | |
| 286 | |
| 287 | |
| 288 | void visitReturnInst(ReturnInst &I); |
| 289 | void visitTerminator(Instruction &TI); |
| 290 | |
| 291 | void visitCastInst(CastInst &I); |
| 292 | void visitSelectInst(SelectInst &I); |
| 293 | void visitUnaryOperator(Instruction &I); |
| 294 | void visitBinaryOperator(Instruction &I); |
| 295 | void visitCmpInst(CmpInst &I); |
| 296 | void visitExtractValueInst(ExtractValueInst &EVI); |
| 297 | void visitInsertValueInst(InsertValueInst &IVI); |
| 298 | |
| 299 | void visitCatchSwitchInst(CatchSwitchInst &CPI) { |
| 300 | markOverdefined(&CPI); |
| 301 | visitTerminator(CPI); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | |
| 306 | void visitStoreInst(StoreInst &I); |
| 307 | void visitLoadInst(LoadInst &I); |
| 308 | void visitGetElementPtrInst(GetElementPtrInst &I); |
| 309 | |
| 310 | void visitInvokeInst(InvokeInst &II) { |
| 311 | visitCallBase(II); |
| 312 | visitTerminator(II); |
| 313 | } |
| 314 | |
| 315 | void visitCallBrInst(CallBrInst &CBI) { |
| 316 | visitCallBase(CBI); |
| 317 | visitTerminator(CBI); |
| 318 | } |
| 319 | |
| 320 | void visitCallBase(CallBase &CB); |
| 321 | void visitResumeInst(ResumeInst &I) { |
| 322 | } |
| 323 | void visitUnreachableInst(UnreachableInst &I) { |
| 324 | } |
| 325 | void visitFenceInst(FenceInst &I) { |
| 326 | } |
| 327 | |
| 328 | void visitInstruction(Instruction &I); |
| 329 | |
| 330 | public: |
| 331 | void addAnalysis(Function &F, AnalysisResultsForFn A) { |
| 332 | AnalysisResults.insert({&F, std::move(A)}); |
| 333 | } |
| 334 | |
| 335 | void visitCallInst(CallInst &I) { visitCallBase(I); } |
| 336 | |
| 337 | bool markBlockExecutable(BasicBlock *BB); |
| 338 | |
| 339 | const PredicateBase *getPredicateInfoFor(Instruction *I) { |
| 340 | auto A = AnalysisResults.find(I->getParent()->getParent()); |
| 341 | if (A == AnalysisResults.end()) |
| 342 | return nullptr; |
| 343 | return A->second.PredInfo->getPredicateInfoFor(I); |
| 344 | } |
| 345 | |
| 346 | DomTreeUpdater getDTU(Function &F) { |
| 347 | auto A = AnalysisResults.find(&F); |
| 348 | assert(A != AnalysisResults.end() && "Need analysis results for function."); |
| 349 | return {A->second.DT, A->second.PDT, DomTreeUpdater::UpdateStrategy::Lazy}; |
| 350 | } |
| 351 | |
| 352 | SCCPInstVisitor(const DataLayout &DL, |
| 353 | std::function<const TargetLibraryInfo &(Function &)> GetTLI, |
| 354 | LLVMContext &Ctx) |
| 355 | : DL(DL), GetTLI(GetTLI), Ctx(Ctx) {} |
| 356 | |
| 357 | void trackValueOfGlobalVariable(GlobalVariable *GV) { |
| 358 | |
| 359 | if (GV->getValueType()->isSingleValueType()) { |
| 360 | ValueLatticeElement &IV = TrackedGlobals[GV]; |
| 361 | if (!isa<UndefValue>(GV->getInitializer())) |
| 362 | IV.markConstant(GV->getInitializer()); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | void addTrackedFunction(Function *F) { |
| 367 | |
| 368 | if (auto *STy = dyn_cast<StructType>(F->getReturnType())) { |
| 369 | MRVFunctionsTracked.insert(F); |
| 370 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
| 371 | TrackedMultipleRetVals.insert( |
| 372 | std::make_pair(std::make_pair(F, i), ValueLatticeElement())); |
| 373 | } else if (!F->getReturnType()->isVoidTy()) |
| 374 | TrackedRetVals.insert(std::make_pair(F, ValueLatticeElement())); |
| 375 | } |
| 376 | |
| 377 | void addToMustPreserveReturnsInFunctions(Function *F) { |
| 378 | MustPreserveReturnsInFunctions.insert(F); |
| 379 | } |
| 380 | |
| 381 | bool mustPreserveReturn(Function *F) { |
| 382 | return MustPreserveReturnsInFunctions.count(F); |
| 383 | } |
| 384 | |
| 385 | void addArgumentTrackedFunction(Function *F) { |
| 386 | TrackingIncomingArguments.insert(F); |
| 387 | } |
| 388 | |
| 389 | bool isArgumentTrackedFunction(Function *F) { |
| 390 | return TrackingIncomingArguments.count(F); |
| 391 | } |
| 392 | |
| 393 | void solve(); |
| 394 | |
| 395 | bool resolvedUndefsIn(Function &F); |
| 396 | |
| 397 | bool isBlockExecutable(BasicBlock *BB) const { |
| 398 | return BBExecutable.count(BB); |
| 399 | } |
| 400 | |
| 401 | bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const; |
| 402 | |
| 403 | std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const { |
| 404 | std::vector<ValueLatticeElement> StructValues; |
| 405 | auto *STy = dyn_cast<StructType>(V->getType()); |
| 2 | | Assuming the object is not a 'StructType' | |
|
| 3 | | 'STy' initialized to a null pointer value | |
|
| 406 | assert(STy && "getStructLatticeValueFor() can be called only on structs"); |
| 407 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 4 | | Called C++ object pointer is null |
|
| 408 | auto I = StructValueState.find(std::make_pair(V, i)); |
| 409 | assert(I != StructValueState.end() && "Value not in valuemap!"); |
| 410 | StructValues.push_back(I->second); |
| 411 | } |
| 412 | return StructValues; |
| 413 | } |
| 414 | |
| 415 | void removeLatticeValueFor(Value *V) { ValueState.erase(V); } |
| 416 | |
| 417 | const ValueLatticeElement &getLatticeValueFor(Value *V) const { |
| 418 | assert(!V->getType()->isStructTy() && |
| 419 | "Should use getStructLatticeValueFor"); |
| 420 | DenseMap<Value *, ValueLatticeElement>::const_iterator I = |
| 421 | ValueState.find(V); |
| 422 | assert(I != ValueState.end() && |
| 423 | "V not found in ValueState nor Paramstate map!"); |
| 424 | return I->second; |
| 425 | } |
| 426 | |
| 427 | const MapVector<Function *, ValueLatticeElement> &getTrackedRetVals() { |
| 428 | return TrackedRetVals; |
| 429 | } |
| 430 | |
| 431 | const DenseMap<GlobalVariable *, ValueLatticeElement> &getTrackedGlobals() { |
| 432 | return TrackedGlobals; |
| 433 | } |
| 434 | |
| 435 | const SmallPtrSet<Function *, 16> getMRVFunctionsTracked() { |
| 436 | return MRVFunctionsTracked; |
| 437 | } |
| 438 | |
| 439 | void markOverdefined(Value *V) { |
| 440 | if (auto *STy = dyn_cast<StructType>(V->getType())) |
| 441 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
| 442 | markOverdefined(getStructValueState(V, i), V); |
| 443 | else |
| 444 | markOverdefined(ValueState[V], V); |
| 445 | } |
| 446 | |
| 447 | bool isStructLatticeConstant(Function *F, StructType *STy); |
| 448 | |
| 449 | Constant *getConstant(const ValueLatticeElement &LV) const; |
| 450 | |
| 451 | SmallPtrSetImpl<Function *> &getArgumentTrackedFunctions() { |
| 452 | return TrackingIncomingArguments; |
| 453 | } |
| 454 | |
| 455 | void markArgInFuncSpecialization(Function *F, Argument *A, Constant *C); |
| 456 | |
| 457 | void markFunctionUnreachable(Function *F) { |
| 458 | for (auto &BB : *F) |
| 459 | BBExecutable.erase(&BB); |
| 460 | } |
| 461 | }; |
| 462 | |
| 463 | } |
| 464 | |
| 465 | bool SCCPInstVisitor::markBlockExecutable(BasicBlock *BB) { |
| 466 | if (!BBExecutable.insert(BB).second) |
| 467 | return false; |
| 468 | LLVM_DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n'); |
| 469 | BBWorkList.push_back(BB); |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | void SCCPInstVisitor::pushToWorkList(ValueLatticeElement &IV, Value *V) { |
| 474 | if (IV.isOverdefined()) |
| 475 | return OverdefinedInstWorkList.push_back(V); |
| 476 | InstWorkList.push_back(V); |
| 477 | } |
| 478 | |
| 479 | void SCCPInstVisitor::pushToWorkListMsg(ValueLatticeElement &IV, Value *V) { |
| 480 | LLVM_DEBUG(dbgs() << "updated " << IV << ": " << *V << '\n'); |
| 481 | pushToWorkList(IV, V); |
| 482 | } |
| 483 | |
| 484 | bool SCCPInstVisitor::markConstant(ValueLatticeElement &IV, Value *V, |
| 485 | Constant *C, bool MayIncludeUndef) { |
| 486 | if (!IV.markConstant(C, MayIncludeUndef)) |
| 487 | return false; |
| 488 | LLVM_DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n'); |
| 489 | pushToWorkList(IV, V); |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | bool SCCPInstVisitor::markOverdefined(ValueLatticeElement &IV, Value *V) { |
| 494 | if (!IV.markOverdefined()) |
| 495 | return false; |
| 496 | |
| 497 | LLVM_DEBUG(dbgs() << "markOverdefined: "; |
| 498 | if (auto *F = dyn_cast<Function>(V)) dbgs() |
| 499 | << "Function '" << F->getName() << "'\n"; |
| 500 | else dbgs() << *V << '\n'); |
| 501 | |
| 502 | pushToWorkList(IV, V); |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | bool SCCPInstVisitor::isStructLatticeConstant(Function *F, StructType *STy) { |
| 507 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 508 | const auto &It = TrackedMultipleRetVals.find(std::make_pair(F, i)); |
| 509 | assert(It != TrackedMultipleRetVals.end()); |
| 510 | ValueLatticeElement LV = It->second; |
| 511 | if (!isConstant(LV)) |
| 512 | return false; |
| 513 | } |
| 514 | return true; |
| 515 | } |
| 516 | |
| 517 | Constant *SCCPInstVisitor::getConstant(const ValueLatticeElement &LV) const { |
| 518 | if (LV.isConstant()) |
| 519 | return LV.getConstant(); |
| 520 | |
| 521 | if (LV.isConstantRange()) { |
| 522 | const auto &CR = LV.getConstantRange(); |
| 523 | if (CR.getSingleElement()) |
| 524 | return ConstantInt::get(Ctx, *CR.getSingleElement()); |
| 525 | } |
| 526 | return nullptr; |
| 527 | } |
| 528 | |
| 529 | void SCCPInstVisitor::markArgInFuncSpecialization(Function *F, Argument *A, |
| 530 | Constant *C) { |
| 531 | assert(F->arg_size() == A->getParent()->arg_size() && |
| 532 | "Functions should have the same number of arguments"); |
| 533 | |
| 534 | |
| 535 | markConstant(A, C); |
| 536 | |
| 537 | |
| 538 | |
| 539 | for (auto I = F->arg_begin(), J = A->getParent()->arg_begin(), |
| 540 | E = F->arg_end(); |
| 541 | I != E; ++I, ++J) |
| 542 | if (J != A && ValueState.count(I)) { |
| 543 | ValueState[J] = ValueState[I]; |
| 544 | pushToWorkList(ValueState[J], J); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | void SCCPInstVisitor::visitInstruction(Instruction &I) { |
| 549 | |
| 550 | |
| 551 | LLVM_DEBUG(dbgs() << "SCCP: Don't know how to handle: " << I << '\n'); |
| 552 | markOverdefined(&I); |
| 553 | } |
| 554 | |
| 555 | bool SCCPInstVisitor::mergeInValue(ValueLatticeElement &IV, Value *V, |
| 556 | ValueLatticeElement MergeWithV, |
| 557 | ValueLatticeElement::MergeOptions Opts) { |
| 558 | if (IV.mergeIn(MergeWithV, Opts)) { |
| 559 | pushToWorkList(IV, V); |
| 560 | LLVM_DEBUG(dbgs() << "Merged " << MergeWithV << " into " << *V << " : " |
| 561 | << IV << "\n"); |
| 562 | return true; |
| 563 | } |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | bool SCCPInstVisitor::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) { |
| 568 | if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second) |
| 569 | return false; |
| 570 | |
| 571 | if (!markBlockExecutable(Dest)) { |
| 572 | |
| 573 | |
| 574 | |
| 575 | LLVM_DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName() |
| 576 | << " -> " << Dest->getName() << '\n'); |
| 577 | |
| 578 | for (PHINode &PN : Dest->phis()) |
| 579 | visitPHINode(PN); |
| 580 | } |
| 581 | return true; |
| 582 | } |
| 583 | |
| 584 | |
| 585 | |
| 586 | void SCCPInstVisitor::getFeasibleSuccessors(Instruction &TI, |
| 587 | SmallVectorImpl<bool> &Succs) { |
| 588 | Succs.resize(TI.getNumSuccessors()); |
| 589 | if (auto *BI = dyn_cast<BranchInst>(&TI)) { |
| 590 | if (BI->isUnconditional()) { |
| 591 | Succs[0] = true; |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | ValueLatticeElement BCValue = getValueState(BI->getCondition()); |
| 596 | ConstantInt *CI = getConstantInt(BCValue); |
| 597 | if (!CI) { |
| 598 | |
| 599 | |
| 600 | if (!BCValue.isUnknownOrUndef()) |
| 601 | Succs[0] = Succs[1] = true; |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | Succs[CI->isZero()] = true; |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | if (TI.isExceptionalTerminator()) { |
| 612 | Succs.assign(TI.getNumSuccessors(), true); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | if (auto *SI = dyn_cast<SwitchInst>(&TI)) { |
| 617 | if (!SI->getNumCases()) { |
| 618 | Succs[0] = true; |
| 619 | return; |
| 620 | } |
| 621 | const ValueLatticeElement &SCValue = getValueState(SI->getCondition()); |
| 622 | if (ConstantInt *CI = getConstantInt(SCValue)) { |
| 623 | Succs[SI->findCaseValue(CI)->getSuccessorIndex()] = true; |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | |
| 629 | if (SCValue.isConstantRange(false)) { |
| 630 | const ConstantRange &Range = SCValue.getConstantRange(); |
| 631 | for (const auto &Case : SI->cases()) { |
| 632 | const APInt &CaseValue = Case.getCaseValue()->getValue(); |
| 633 | if (Range.contains(CaseValue)) |
| 634 | Succs[Case.getSuccessorIndex()] = true; |
| 635 | } |
| 636 | |
| 637 | |
| 638 | Succs[SI->case_default()->getSuccessorIndex()] = true; |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | |
| 643 | if (!SCValue.isUnknownOrUndef()) |
| 644 | Succs.assign(TI.getNumSuccessors(), true); |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | |
| 650 | if (auto *IBR = dyn_cast<IndirectBrInst>(&TI)) { |
| 651 | |
| 652 | ValueLatticeElement IBRValue = getValueState(IBR->getAddress()); |
| 653 | BlockAddress *Addr = dyn_cast_or_null<BlockAddress>(getConstant(IBRValue)); |
| 654 | if (!Addr) { |
| 655 | |
| 656 | if (!IBRValue.isUnknownOrUndef()) |
| 657 | Succs.assign(TI.getNumSuccessors(), true); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | BasicBlock *T = Addr->getBasicBlock(); |
| 662 | assert(Addr->getFunction() == T->getParent() && |
| 663 | "Block address of a different function ?"); |
| 664 | for (unsigned i = 0; i < IBR->getNumSuccessors(); ++i) { |
| 665 | |
| 666 | if (IBR->getDestination(i) == T) { |
| 667 | Succs[i] = true; |
| 668 | return; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | |
| 673 | |
| 674 | return; |
| 675 | } |
| 676 | |
| 677 | |
| 678 | |
| 679 | if (isa<CallBrInst>(&TI)) { |
| 680 | Succs.assign(TI.getNumSuccessors(), true); |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | LLVM_DEBUG(dbgs() << "Unknown terminator instruction: " << TI << '\n'); |
| 685 | llvm_unreachable("SCCP: Don't know how to handle this terminator!"); |
| 686 | } |
| 687 | |
| 688 | |
| 689 | |
| 690 | bool SCCPInstVisitor::isEdgeFeasible(BasicBlock *From, BasicBlock *To) const { |
| 691 | |
| 692 | |
| 693 | |
| 694 | return KnownFeasibleEdges.count(Edge(From, To)); |
| 695 | } |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | void SCCPInstVisitor::visitPHINode(PHINode &PN) { |
| 715 | |
| 716 | |
| 717 | if (PN.getType()->isStructTy()) |
| 718 | return (void)markOverdefined(&PN); |
| 719 | |
| 720 | if (getValueState(&PN).isOverdefined()) |
| 721 | return; |
| 722 | |
| 723 | |
| 724 | |
| 725 | if (PN.getNumIncomingValues() > 64) |
| 726 | return (void)markOverdefined(&PN); |
| 727 | |
| 728 | unsigned NumActiveIncoming = 0; |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | ValueLatticeElement PhiState = getValueState(&PN); |
| 736 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 737 | if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) |
| 738 | continue; |
| 739 | |
| 740 | ValueLatticeElement IV = getValueState(PN.getIncomingValue(i)); |
| 741 | PhiState.mergeIn(IV); |
| 742 | NumActiveIncoming++; |
| 743 | if (PhiState.isOverdefined()) |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | mergeInValue(&PN, PhiState, |
| 753 | ValueLatticeElement::MergeOptions().setMaxWidenSteps( |
| 754 | NumActiveIncoming + 1)); |
| 755 | ValueLatticeElement &PhiStateRef = getValueState(&PN); |
| 756 | PhiStateRef.setNumRangeExtensions( |
| 757 | std::max(NumActiveIncoming, PhiStateRef.getNumRangeExtensions())); |
| 758 | } |
| 759 | |
| 760 | void SCCPInstVisitor::visitReturnInst(ReturnInst &I) { |
| 761 | if (I.getNumOperands() == 0) |
| 762 | return; |
| 763 | |
| 764 | Function *F = I.getParent()->getParent(); |
| 765 | Value *ResultOp = I.getOperand(0); |
| 766 | |
| 767 | |
| 768 | if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) { |
| 769 | auto TFRVI = TrackedRetVals.find(F); |
| 770 | if (TFRVI != TrackedRetVals.end()) { |
| 771 | mergeInValue(TFRVI->second, F, getValueState(ResultOp)); |
| 772 | return; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | |
| 777 | if (!TrackedMultipleRetVals.empty()) { |
| 778 | if (auto *STy = dyn_cast<StructType>(ResultOp->getType())) |
| 779 | if (MRVFunctionsTracked.count(F)) |
| 780 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
| 781 | mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F, |
| 782 | getStructValueState(ResultOp, i)); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | void SCCPInstVisitor::visitTerminator(Instruction &TI) { |
| 787 | SmallVector<bool, 16> SuccFeasible; |
| 788 | getFeasibleSuccessors(TI, SuccFeasible); |
| 789 | |
| 790 | BasicBlock *BB = TI.getParent(); |
| 791 | |
| 792 | |
| 793 | for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i) |
| 794 | if (SuccFeasible[i]) |
| 795 | markEdgeExecutable(BB, TI.getSuccessor(i)); |
| 796 | } |
| 797 | |
| 798 | void SCCPInstVisitor::visitCastInst(CastInst &I) { |
| 799 | |
| 800 | |
| 801 | if (ValueState[&I].isOverdefined()) |
| 802 | return; |
| 803 | |
| 804 | ValueLatticeElement OpSt = getValueState(I.getOperand(0)); |
| 805 | if (Constant *OpC = getConstant(OpSt)) { |
| 806 | |
| 807 | Constant *C = ConstantFoldCastOperand(I.getOpcode(), OpC, I.getType(), DL); |
| 808 | if (isa<UndefValue>(C)) |
| 809 | return; |
| 810 | |
| 811 | markConstant(&I, C); |
| 812 | } else if (OpSt.isConstantRange() && I.getDestTy()->isIntegerTy()) { |
| 813 | auto &LV = getValueState(&I); |
| 814 | ConstantRange OpRange = OpSt.getConstantRange(); |
| 815 | Type *DestTy = I.getDestTy(); |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | if (I.getOpcode() == Instruction::BitCast && |
| 822 | I.getOperand(0)->getType()->isVectorTy() && |
| 823 | OpRange.getBitWidth() < DL.getTypeSizeInBits(DestTy)) |
| 824 | return (void)markOverdefined(&I); |
| 825 | |
| 826 | ConstantRange Res = |
| 827 | OpRange.castOp(I.getOpcode(), DL.getTypeSizeInBits(DestTy)); |
| 828 | mergeInValue(LV, &I, ValueLatticeElement::getRange(Res)); |
| 829 | } else if (!OpSt.isUnknownOrUndef()) |
| 830 | markOverdefined(&I); |
| 831 | } |
| 832 | |
| 833 | void SCCPInstVisitor::visitExtractValueInst(ExtractValueInst &EVI) { |
| 834 | |
| 835 | |
| 836 | if (EVI.getType()->isStructTy()) |
| 837 | return (void)markOverdefined(&EVI); |
| 838 | |
| 839 | |
| 840 | |
| 841 | if (ValueState[&EVI].isOverdefined()) |
| 842 | return (void)markOverdefined(&EVI); |
| 843 | |
| 844 | |
| 845 | if (EVI.getNumIndices() != 1) |
| 846 | return (void)markOverdefined(&EVI); |
| 847 | |
| 848 | Value *AggVal = EVI.getAggregateOperand(); |
| 849 | if (AggVal->getType()->isStructTy()) { |
| 850 | unsigned i = *EVI.idx_begin(); |
| 851 | ValueLatticeElement EltVal = getStructValueState(AggVal, i); |
| 852 | mergeInValue(getValueState(&EVI), &EVI, EltVal); |
| 853 | } else { |
| 854 | |
| 855 | return (void)markOverdefined(&EVI); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | void SCCPInstVisitor::visitInsertValueInst(InsertValueInst &IVI) { |
| 860 | auto *STy = dyn_cast<StructType>(IVI.getType()); |
| 861 | if (!STy) |
| 862 | return (void)markOverdefined(&IVI); |
| 863 | |
| 864 | |
| 865 | |
| 866 | if (isOverdefined(ValueState[&IVI])) |
| 867 | return (void)markOverdefined(&IVI); |
| 868 | |
| 869 | |
| 870 | |
| 871 | if (IVI.getNumIndices() != 1) |
| 872 | return (void)markOverdefined(&IVI); |
| 873 | |
| 874 | Value *Aggr = IVI.getAggregateOperand(); |
| 875 | unsigned Idx = *IVI.idx_begin(); |
| 876 | |
| 877 | |
| 878 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 879 | |
| 880 | if (i != Idx) { |
| 881 | ValueLatticeElement EltVal = getStructValueState(Aggr, i); |
| 882 | mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal); |
| 883 | continue; |
| 884 | } |
| 885 | |
| 886 | Value *Val = IVI.getInsertedValueOperand(); |
| 887 | if (Val->getType()->isStructTy()) |
| 888 | |
| 889 | markOverdefined(getStructValueState(&IVI, i), &IVI); |
| 890 | else { |
| 891 | ValueLatticeElement InVal = getValueState(Val); |
| 892 | mergeInValue(getStructValueState(&IVI, i), &IVI, InVal); |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | void SCCPInstVisitor::visitSelectInst(SelectInst &I) { |
| 898 | |
| 899 | |
| 900 | if (I.getType()->isStructTy()) |
| 901 | return (void)markOverdefined(&I); |
| 902 | |
| 903 | |
| 904 | |
| 905 | if (ValueState[&I].isOverdefined()) |
| 906 | return (void)markOverdefined(&I); |
| 907 | |
| 908 | ValueLatticeElement CondValue = getValueState(I.getCondition()); |
| 909 | if (CondValue.isUnknownOrUndef()) |
| 910 | return; |
| 911 | |
| 912 | if (ConstantInt *CondCB = getConstantInt(CondValue)) { |
| 913 | Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue(); |
| 914 | mergeInValue(&I, getValueState(OpVal)); |
| 915 | return; |
| 916 | } |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | ValueLatticeElement TVal = getValueState(I.getTrueValue()); |
| 922 | ValueLatticeElement FVal = getValueState(I.getFalseValue()); |
| 923 | |
| 924 | bool Changed = ValueState[&I].mergeIn(TVal); |
| 925 | Changed |= ValueState[&I].mergeIn(FVal); |
| 926 | if (Changed) |
| 927 | pushToWorkListMsg(ValueState[&I], &I); |
| 928 | } |
| 929 | |
| 930 | |
| 931 | void SCCPInstVisitor::visitUnaryOperator(Instruction &I) { |
| 932 | ValueLatticeElement V0State = getValueState(I.getOperand(0)); |
| 933 | |
| 934 | ValueLatticeElement &IV = ValueState[&I]; |
| 935 | |
| 936 | |
| 937 | if (isOverdefined(IV)) |
| 938 | return (void)markOverdefined(&I); |
| 939 | |
| 940 | if (isConstant(V0State)) { |
| 941 | Constant *C = ConstantExpr::get(I.getOpcode(), getConstant(V0State)); |
| 942 | |
| 943 | |
| 944 | if (isa<UndefValue>(C)) |
| 945 | return; |
| 946 | return (void)markConstant(IV, &I, C); |
| 947 | } |
| 948 | |
| 949 | |
| 950 | if (!isOverdefined(V0State)) |
| 951 | return; |
| 952 | |
| 953 | markOverdefined(&I); |
| 954 | } |
| 955 | |
| 956 | |
| 957 | void SCCPInstVisitor::visitBinaryOperator(Instruction &I) { |
| 958 | ValueLatticeElement V1State = getValueState(I.getOperand(0)); |
| 959 | ValueLatticeElement V2State = getValueState(I.getOperand(1)); |
| 960 | |
| 961 | ValueLatticeElement &IV = ValueState[&I]; |
| 962 | if (IV.isOverdefined()) |
| 963 | return; |
| 964 | |
| 965 | |
| 966 | if (V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef()) |
| 967 | return; |
| 968 | |
| 969 | if (V1State.isOverdefined() && V2State.isOverdefined()) |
| 970 | return (void)markOverdefined(&I); |
| 971 | |
| 972 | |
| 973 | |
| 974 | if ((V1State.isConstant() || V2State.isConstant())) { |
| 975 | Value *V1 = isConstant(V1State) ? getConstant(V1State) : I.getOperand(0); |
| 976 | Value *V2 = isConstant(V2State) ? getConstant(V2State) : I.getOperand(1); |
| 977 | Value *R = SimplifyBinOp(I.getOpcode(), V1, V2, SimplifyQuery(DL)); |
| 978 | auto *C = dyn_cast_or_null<Constant>(R); |
| 979 | if (C) { |
| 980 | |
| 981 | if (isa<UndefValue>(C)) |
| 982 | return; |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | ValueLatticeElement NewV; |
| 989 | NewV.markConstant(C, true); |
| 990 | return (void)mergeInValue(&I, NewV); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | |
| 995 | if (!I.getType()->isIntegerTy()) |
| 996 | return markOverdefined(&I); |
| 997 | |
| 998 | |
| 999 | ConstantRange A = ConstantRange::getFull(I.getType()->getScalarSizeInBits()); |
| 1000 | ConstantRange B = ConstantRange::getFull(I.getType()->getScalarSizeInBits()); |
| 1001 | if (V1State.isConstantRange()) |
| 1002 | A = V1State.getConstantRange(); |
| 1003 | if (V2State.isConstantRange()) |
| 1004 | B = V2State.getConstantRange(); |
| 1005 | |
| 1006 | ConstantRange R = A.binaryOp(cast<BinaryOperator>(&I)->getOpcode(), B); |
| 1007 | mergeInValue(&I, ValueLatticeElement::getRange(R)); |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | void SCCPInstVisitor::visitCmpInst(CmpInst &I) { |
| 1016 | |
| 1017 | |
| 1018 | if (isOverdefined(ValueState[&I])) |
| 1019 | return (void)markOverdefined(&I); |
| 1020 | |
| 1021 | Value *Op1 = I.getOperand(0); |
| 1022 | Value *Op2 = I.getOperand(1); |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | auto V1State = getValueState(Op1); |
| 1027 | auto V2State = getValueState(Op2); |
| 1028 | |
| 1029 | Constant *C = V1State.getCompare(I.getPredicate(), I.getType(), V2State); |
| 1030 | if (C) { |
| 1031 | if (isa<UndefValue>(C)) |
| 1032 | return; |
| 1033 | ValueLatticeElement CV; |
| 1034 | CV.markConstant(C); |
| 1035 | mergeInValue(&I, CV); |
| 1036 | return; |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | if ((V1State.isUnknownOrUndef() || V2State.isUnknownOrUndef()) && |
| 1041 | !isConstant(ValueState[&I])) |
| 1042 | return; |
| 1043 | |
| 1044 | markOverdefined(&I); |
| 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | void SCCPInstVisitor::visitGetElementPtrInst(GetElementPtrInst &I) { |
| 1050 | if (isOverdefined(ValueState[&I])) |
| 1051 | return (void)markOverdefined(&I); |
| 1052 | |
| 1053 | SmallVector<Constant *, 8> Operands; |
| 1054 | Operands.reserve(I.getNumOperands()); |
| 1055 | |
| 1056 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 1057 | ValueLatticeElement State = getValueState(I.getOperand(i)); |
| 1058 | if (State.isUnknownOrUndef()) |
| 1059 | return; |
| 1060 | |
| 1061 | if (isOverdefined(State)) |
| 1062 | return (void)markOverdefined(&I); |
| 1063 | |
| 1064 | if (Constant *C = getConstant(State)) { |
| 1065 | Operands.push_back(C); |
| 1066 | continue; |
| 1067 | } |
| 1068 | |
| 1069 | return (void)markOverdefined(&I); |
| 1070 | } |
| 1071 | |
| 1072 | Constant *Ptr = Operands[0]; |
| 1073 | auto Indices = makeArrayRef(Operands.begin() + 1, Operands.end()); |
| 1074 | Constant *C = |
| 1075 | ConstantExpr::getGetElementPtr(I.getSourceElementType(), Ptr, Indices); |
| 1076 | if (isa<UndefValue>(C)) |
| 1077 | return; |
| 1078 | markConstant(&I, C); |
| 1079 | } |
| 1080 | |
| 1081 | void SCCPInstVisitor::visitStoreInst(StoreInst &SI) { |
| 1082 | |
| 1083 | if (SI.getOperand(0)->getType()->isStructTy()) |
| 1084 | return; |
| 1085 | |
| 1086 | if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1))) |
| 1087 | return; |
| 1088 | |
| 1089 | GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1)); |
| 1090 | auto I = TrackedGlobals.find(GV); |
| 1091 | if (I == TrackedGlobals.end()) |
| 1092 | return; |
| 1093 | |
| 1094 | |
| 1095 | mergeInValue(I->second, GV, getValueState(SI.getOperand(0)), |
| 1096 | ValueLatticeElement::MergeOptions().setCheckWiden(false)); |
| 1097 | if (I->second.isOverdefined()) |
| 1098 | TrackedGlobals.erase(I); |
| 1099 | } |
| 1100 | |
| 1101 | static ValueLatticeElement getValueFromMetadata(const Instruction *I) { |
| 1102 | if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range)) |
| 1103 | if (I->getType()->isIntegerTy()) |
| 1104 | return ValueLatticeElement::getRange( |
| 1105 | getConstantRangeFromMetadata(*Ranges)); |
| 1106 | if (I->hasMetadata(LLVMContext::MD_nonnull)) |
| 1107 | return ValueLatticeElement::getNot( |
| 1108 | ConstantPointerNull::get(cast<PointerType>(I->getType()))); |
| 1109 | return ValueLatticeElement::getOverdefined(); |
| 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | void SCCPInstVisitor::visitLoadInst(LoadInst &I) { |
| 1115 | |
| 1116 | |
| 1117 | if (I.getType()->isStructTy() || I.isVolatile()) |
| 1118 | return (void)markOverdefined(&I); |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | if (ValueState[&I].isOverdefined()) |
| 1123 | return (void)markOverdefined(&I); |
| 1124 | |
| 1125 | ValueLatticeElement PtrVal = getValueState(I.getOperand(0)); |
| 1126 | if (PtrVal.isUnknownOrUndef()) |
| 1127 | return; |
| 1128 | |
| 1129 | ValueLatticeElement &IV = ValueState[&I]; |
| 1130 | |
| 1131 | if (isConstant(PtrVal)) { |
| 1132 | Constant *Ptr = getConstant(PtrVal); |
| 1133 | |
| 1134 | |
| 1135 | if (isa<ConstantPointerNull>(Ptr)) { |
| 1136 | if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace())) |
| 1137 | return (void)markOverdefined(IV, &I); |
| 1138 | else |
| 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) { |
| 1144 | if (!TrackedGlobals.empty()) { |
| 1145 | |
| 1146 | auto It = TrackedGlobals.find(GV); |
| 1147 | if (It != TrackedGlobals.end()) { |
| 1148 | mergeInValue(IV, &I, It->second, getMaxWidenStepsOpts()); |
| 1149 | return; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL)) { |
| 1156 | if (isa<UndefValue>(C)) |
| 1157 | return; |
| 1158 | return (void)markConstant(IV, &I, C); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | mergeInValue(&I, getValueFromMetadata(&I)); |
| 1164 | } |
| 1165 | |
| 1166 | void SCCPInstVisitor::visitCallBase(CallBase &CB) { |
| 1167 | handleCallResult(CB); |
| 1168 | handleCallArguments(CB); |
| 1169 | } |
| 1170 | |
| 1171 | void SCCPInstVisitor::handleCallOverdefined(CallBase &CB) { |
| 1172 | Function *F = CB.getCalledFunction(); |
| 1173 | |
| 1174 | |
| 1175 | if (CB.getType()->isVoidTy()) |
| 1176 | return; |
| 1177 | |
| 1178 | |
| 1179 | if (CB.getType()->isStructTy()) |
| 1180 | return (void)markOverdefined(&CB); |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | if (F && F->isDeclaration() && canConstantFoldCallTo(&CB, F)) { |
| 1185 | SmallVector<Constant *, 8> Operands; |
| 1186 | for (auto AI = CB.arg_begin(), E = CB.arg_end(); AI != E; ++AI) { |
| 1187 | if (AI->get()->getType()->isStructTy()) |
| 1188 | return markOverdefined(&CB); |
| 1189 | ValueLatticeElement State = getValueState(*AI); |
| 1190 | |
| 1191 | if (State.isUnknownOrUndef()) |
| 1192 | return; |
| 1193 | if (isOverdefined(State)) |
| 1194 | return (void)markOverdefined(&CB); |
| 1195 | assert(isConstant(State) && "Unknown state!"); |
| 1196 | Operands.push_back(getConstant(State)); |
| 1197 | } |
| 1198 | |
| 1199 | if (isOverdefined(getValueState(&CB))) |
| 1200 | return (void)markOverdefined(&CB); |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | if (Constant *C = ConstantFoldCall(&CB, F, Operands, &GetTLI(*F))) { |
| 1205 | |
| 1206 | if (isa<UndefValue>(C)) |
| 1207 | return; |
| 1208 | return (void)markConstant(&CB, C); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | mergeInValue(&CB, getValueFromMetadata(&CB)); |
| 1214 | } |
| 1215 | |
| 1216 | void SCCPInstVisitor::handleCallArguments(CallBase &CB) { |
| 1217 | Function *F = CB.getCalledFunction(); |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | if (!TrackingIncomingArguments.empty() && |
| 1222 | TrackingIncomingArguments.count(F)) { |
| 1223 | markBlockExecutable(&F->front()); |
| 1224 | |
| 1225 | |
| 1226 | auto CAI = CB.arg_begin(); |
| 1227 | for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; |
| 1228 | ++AI, ++CAI) { |
| 1229 | |
| 1230 | |
| 1231 | if (AI->hasByValAttr() && !F->onlyReadsMemory()) { |
| 1232 | markOverdefined(&*AI); |
| 1233 | continue; |
| 1234 | } |
| 1235 | |
| 1236 | if (auto *STy = dyn_cast<StructType>(AI->getType())) { |
| 1237 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1238 | ValueLatticeElement CallArg = getStructValueState(*CAI, i); |
| 1239 | mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg, |
| 1240 | getMaxWidenStepsOpts()); |
| 1241 | } |
| 1242 | } else |
| 1243 | mergeInValue(&*AI, getValueState(*CAI), getMaxWidenStepsOpts()); |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | void SCCPInstVisitor::handleCallResult(CallBase &CB) { |
| 1249 | Function *F = CB.getCalledFunction(); |
| 1250 | |
| 1251 | if (auto *II = dyn_cast<IntrinsicInst>(&CB)) { |
| 1252 | if (II->getIntrinsicID() == Intrinsic::ssa_copy) { |
| 1253 | if (ValueState[&CB].isOverdefined()) |
| 1254 | return; |
| 1255 | |
| 1256 | Value *CopyOf = CB.getOperand(0); |
| 1257 | ValueLatticeElement CopyOfVal = getValueState(CopyOf); |
| 1258 | const auto *PI = getPredicateInfoFor(&CB); |
| 1259 | assert(PI && "Missing predicate info for ssa.copy"); |
| 1260 | |
| 1261 | const Optional<PredicateConstraint> &Constraint = PI->getConstraint(); |
| 1262 | if (!Constraint) { |
| 1263 | mergeInValue(ValueState[&CB], &CB, CopyOfVal); |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | CmpInst::Predicate Pred = Constraint->Predicate; |
| 1268 | Value *OtherOp = Constraint->OtherOp; |
| 1269 | |
| 1270 | |
| 1271 | if (getValueState(OtherOp).isUnknown()) { |
| 1272 | addAdditionalUser(OtherOp, &CB); |
| 1273 | return; |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | bool MayIncludeUndef = !isa<PredicateAssume>(PI); |
| 1286 | |
| 1287 | ValueLatticeElement CondVal = getValueState(OtherOp); |
| 1288 | ValueLatticeElement &IV = ValueState[&CB]; |
| 1289 | if (CondVal.isConstantRange() || CopyOfVal.isConstantRange()) { |
| 1290 | auto ImposedCR = |
| 1291 | ConstantRange::getFull(DL.getTypeSizeInBits(CopyOf->getType())); |
| 1292 | |
| 1293 | |
| 1294 | if (CondVal.isConstantRange()) |
| 1295 | ImposedCR = ConstantRange::makeAllowedICmpRegion( |
| 1296 | Pred, CondVal.getConstantRange()); |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | auto CopyOfCR = CopyOfVal.isConstantRange() |
| 1301 | ? CopyOfVal.getConstantRange() |
| 1302 | : ConstantRange::getFull( |
| 1303 | DL.getTypeSizeInBits(CopyOf->getType())); |
| 1304 | auto NewCR = ImposedCR.intersectWith(CopyOfCR); |
| 1305 | |
| 1306 | |
| 1307 | |
| 1308 | if (!CopyOfCR.contains(NewCR) && CopyOfCR.getSingleMissingElement()) |
| 1309 | NewCR = CopyOfCR; |
| 1310 | |
| 1311 | addAdditionalUser(OtherOp, &CB); |
| 1312 | mergeInValue(IV, &CB, |
| 1313 | ValueLatticeElement::getRange(NewCR, MayIncludeUndef)); |
| 1314 | return; |
| 1315 | } else if (Pred == CmpInst::ICMP_EQ && CondVal.isConstant()) { |
| 1316 | |
| 1317 | |
| 1318 | addAdditionalUser(OtherOp, &CB); |
| 1319 | mergeInValue(IV, &CB, CondVal); |
| 1320 | return; |
| 1321 | } else if (Pred == CmpInst::ICMP_NE && CondVal.isConstant() && |
| 1322 | !MayIncludeUndef) { |
| 1323 | |
| 1324 | addAdditionalUser(OtherOp, &CB); |
| 1325 | mergeInValue(IV, &CB, |
| 1326 | ValueLatticeElement::getNot(CondVal.getConstant())); |
| 1327 | return; |
| 1328 | } |
| 1329 | |
| 1330 | return (void)mergeInValue(IV, &CB, CopyOfVal); |
| 1331 | } |
| 1332 | |
| 1333 | if (ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) { |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | SmallVector<ConstantRange, 2> OpRanges; |
| 1338 | for (Value *Op : II->args()) { |
| 1339 | const ValueLatticeElement &State = getValueState(Op); |
| 1340 | if (State.isConstantRange()) |
| 1341 | OpRanges.push_back(State.getConstantRange()); |
| 1342 | else |
| 1343 | OpRanges.push_back( |
| 1344 | ConstantRange::getFull(Op->getType()->getScalarSizeInBits())); |
| 1345 | } |
| 1346 | |
| 1347 | ConstantRange Result = |
| 1348 | ConstantRange::intrinsic(II->getIntrinsicID(), OpRanges); |
| 1349 | return (void)mergeInValue(II, ValueLatticeElement::getRange(Result)); |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | |
| 1354 | |
| 1355 | |
| 1356 | if (!F || F->isDeclaration()) |
| 1357 | return handleCallOverdefined(CB); |
| 1358 | |
| 1359 | |
| 1360 | if (auto *STy = dyn_cast<StructType>(F->getReturnType())) { |
| 1361 | if (!MRVFunctionsTracked.count(F)) |
| 1362 | return handleCallOverdefined(CB); |
| 1363 | |
| 1364 | |
| 1365 | |
| 1366 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
| 1367 | mergeInValue(getStructValueState(&CB, i), &CB, |
| 1368 | TrackedMultipleRetVals[std::make_pair(F, i)], |
| 1369 | getMaxWidenStepsOpts()); |
| 1370 | } else { |
| 1371 | auto TFRVI = TrackedRetVals.find(F); |
| 1372 | if (TFRVI == TrackedRetVals.end()) |
| 1373 | return handleCallOverdefined(CB); |
| 1374 | |
| 1375 | |
| 1376 | mergeInValue(&CB, TFRVI->second, getMaxWidenStepsOpts()); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | void SCCPInstVisitor::solve() { |
| 1381 | |
| 1382 | while (!BBWorkList.empty() || !InstWorkList.empty() || |
| 1383 | !OverdefinedInstWorkList.empty()) { |
| 1384 | |
| 1385 | |
| 1386 | while (!OverdefinedInstWorkList.empty()) { |
| 1387 | Value *I = OverdefinedInstWorkList.pop_back_val(); |
| 1388 | |
| 1389 | LLVM_DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n'); |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | markUsersAsChanged(I); |
| 1399 | } |
| 1400 | |
| 1401 | |
| 1402 | while (!InstWorkList.empty()) { |
| 1403 | Value *I = InstWorkList.pop_back_val(); |
| 1404 | |
| 1405 | LLVM_DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n'); |
| 1406 | |
| 1407 | |
| 1408 | |
| 1409 | |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | if (I->getType()->isStructTy() || !getValueState(I).isOverdefined()) |
| 1415 | markUsersAsChanged(I); |
| 1416 | } |
| 1417 | |
| 1418 | |
| 1419 | while (!BBWorkList.empty()) { |
| 1420 | BasicBlock *BB = BBWorkList.pop_back_val(); |
| 1421 | |
| 1422 | LLVM_DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n'); |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | visit(BB); |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | |
| 1435 | |
| 1436 | |
| 1437 | |
| 1438 | |
| 1439 | |
| 1440 | |
| 1441 | |
| 1442 | |
| 1443 | |
| 1444 | |
| 1445 | |
| 1446 | |
| 1447 | bool SCCPInstVisitor::resolvedUndefsIn(Function &F) { |
| 1448 | bool MadeChange = false; |
| 1449 | for (BasicBlock &BB : F) { |
| 1450 | if (!BBExecutable.count(&BB)) |
| 1451 | continue; |
| 1452 | |
| 1453 | for (Instruction &I : BB) { |
| 1454 | |
| 1455 | if (I.getType()->isVoidTy()) |
| 1456 | continue; |
| 1457 | |
| 1458 | if (auto *STy = dyn_cast<StructType>(I.getType())) { |
| 1459 | |
| 1460 | |
| 1461 | |
| 1462 | if (auto *CB = dyn_cast<CallBase>(&I)) |
| 1463 | if (Function *F = CB->getCalledFunction()) |
| 1464 | if (MRVFunctionsTracked.count(F)) |
| 1465 | continue; |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I)) |
| 1470 | continue; |
| 1471 | |
| 1472 | |
| 1473 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1474 | ValueLatticeElement &LV = getStructValueState(&I, i); |
| 1475 | if (LV.isUnknownOrUndef()) { |
| 1476 | markOverdefined(LV, &I); |
| 1477 | MadeChange = true; |
| 1478 | } |
| 1479 | } |
| 1480 | continue; |
| 1481 | } |
| 1482 | |
| 1483 | ValueLatticeElement &LV = getValueState(&I); |
| 1484 | if (!LV.isUnknownOrUndef()) |
| 1485 | continue; |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | |
| 1491 | |
| 1492 | if (auto *CB = dyn_cast<CallBase>(&I)) |
| 1493 | if (Function *F = CB->getCalledFunction()) |
| 1494 | if (TrackedRetVals.count(F)) |
| 1495 | continue; |
| 1496 | |
| 1497 | if (isa<LoadInst>(I)) { |
| 1498 | |
| 1499 | |
| 1500 | |
| 1501 | continue; |
| 1502 | } |
| 1503 | |
| 1504 | markOverdefined(&I); |
| 1505 | MadeChange = true; |
| 1506 | } |
| 1507 | |
| 1508 | |
| 1509 | |
| 1510 | |
| 1511 | Instruction *TI = BB.getTerminator(); |
| 1512 | if (auto *BI = dyn_cast<BranchInst>(TI)) { |
| 1513 | if (!BI->isConditional()) |
| 1514 | continue; |
| 1515 | if (!getValueState(BI->getCondition()).isUnknownOrUndef()) |
| 1516 | continue; |
| 1517 | |
| 1518 | |
| 1519 | |
| 1520 | if (isa<UndefValue>(BI->getCondition())) { |
| 1521 | BI->setCondition(ConstantInt::getFalse(BI->getContext())); |
| 1522 | markEdgeExecutable(&BB, TI->getSuccessor(1)); |
| 1523 | MadeChange = true; |
| 1524 | continue; |
| 1525 | } |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | BasicBlock *DefaultSuccessor = TI->getSuccessor(1); |
| 1532 | if (markEdgeExecutable(&BB, DefaultSuccessor)) |
| 1533 | MadeChange = true; |
| 1534 | |
| 1535 | continue; |
| 1536 | } |
| 1537 | |
| 1538 | if (auto *IBR = dyn_cast<IndirectBrInst>(TI)) { |
| 1539 | |
| 1540 | |
| 1541 | if (IBR->getNumSuccessors() < 1) |
| 1542 | continue; |
| 1543 | |
| 1544 | if (!getValueState(IBR->getAddress()).isUnknownOrUndef()) |
| 1545 | continue; |
| 1546 | |
| 1547 | |
| 1548 | |
| 1549 | if (isa<UndefValue>(IBR->getAddress())) { |
| 1550 | IBR->setAddress(BlockAddress::get(IBR->getSuccessor(0))); |
| 1551 | markEdgeExecutable(&BB, IBR->getSuccessor(0)); |
| 1552 | MadeChange = true; |
| 1553 | continue; |
| 1554 | } |
| 1555 | |
| 1556 | |
| 1557 | |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | BasicBlock *DefaultSuccessor = IBR->getSuccessor(0); |
| 1562 | if (markEdgeExecutable(&BB, DefaultSuccessor)) |
| 1563 | MadeChange = true; |
| 1564 | |
| 1565 | continue; |
| 1566 | } |
| 1567 | |
| 1568 | if (auto *SI = dyn_cast<SwitchInst>(TI)) { |
| 1569 | if (!SI->getNumCases() || |
| 1570 | !getValueState(SI->getCondition()).isUnknownOrUndef()) |
| 1571 | continue; |
| 1572 | |
| 1573 | |
| 1574 | |
| 1575 | if (isa<UndefValue>(SI->getCondition())) { |
| 1576 | SI->setCondition(SI->case_begin()->getCaseValue()); |
| 1577 | markEdgeExecutable(&BB, SI->case_begin()->getCaseSuccessor()); |
| 1578 | MadeChange = true; |
| 1579 | continue; |
| 1580 | } |
| 1581 | |
| 1582 | |
| 1583 | |
| 1584 | |
| 1585 | |
| 1586 | BasicBlock *DefaultSuccessor = SI->case_begin()->getCaseSuccessor(); |
| 1587 | if (markEdgeExecutable(&BB, DefaultSuccessor)) |
| 1588 | MadeChange = true; |
| 1589 | |
| 1590 | continue; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | return MadeChange; |
| 1595 | } |
| 1596 | |
| 1597 | |
| 1598 | |
| 1599 | |
| 1600 | |
| 1601 | SCCPSolver::SCCPSolver( |
| 1602 | const DataLayout &DL, |
| 1603 | std::function<const TargetLibraryInfo &(Function &)> GetTLI, |
| 1604 | LLVMContext &Ctx) |
| 1605 | : Visitor(new SCCPInstVisitor(DL, std::move(GetTLI), Ctx)) {} |
| 1606 | |
| 1607 | SCCPSolver::~SCCPSolver() {} |
| 1608 | |
| 1609 | void SCCPSolver::addAnalysis(Function &F, AnalysisResultsForFn A) { |
| 1610 | return Visitor->addAnalysis(F, std::move(A)); |
| 1611 | } |
| 1612 | |
| 1613 | bool SCCPSolver::markBlockExecutable(BasicBlock *BB) { |
| 1614 | return Visitor->markBlockExecutable(BB); |
| 1615 | } |
| 1616 | |
| 1617 | const PredicateBase *SCCPSolver::getPredicateInfoFor(Instruction *I) { |
| 1618 | return Visitor->getPredicateInfoFor(I); |
| 1619 | } |
| 1620 | |
| 1621 | DomTreeUpdater SCCPSolver::getDTU(Function &F) { return Visitor->getDTU(F); } |
| 1622 | |
| 1623 | void SCCPSolver::trackValueOfGlobalVariable(GlobalVariable *GV) { |
| 1624 | Visitor->trackValueOfGlobalVariable(GV); |
| 1625 | } |
| 1626 | |
| 1627 | void SCCPSolver::addTrackedFunction(Function *F) { |
| 1628 | Visitor->addTrackedFunction(F); |
| 1629 | } |
| 1630 | |
| 1631 | void SCCPSolver::addToMustPreserveReturnsInFunctions(Function *F) { |
| 1632 | Visitor->addToMustPreserveReturnsInFunctions(F); |
| 1633 | } |
| 1634 | |
| 1635 | bool SCCPSolver::mustPreserveReturn(Function *F) { |
| 1636 | return Visitor->mustPreserveReturn(F); |
| 1637 | } |
| 1638 | |
| 1639 | void SCCPSolver::addArgumentTrackedFunction(Function *F) { |
| 1640 | Visitor->addArgumentTrackedFunction(F); |
| 1641 | } |
| 1642 | |
| 1643 | bool SCCPSolver::isArgumentTrackedFunction(Function *F) { |
| 1644 | return Visitor->isArgumentTrackedFunction(F); |
| 1645 | } |
| 1646 | |
| 1647 | void SCCPSolver::solve() { Visitor->solve(); } |
| 1648 | |
| 1649 | bool SCCPSolver::resolvedUndefsIn(Function &F) { |
| 1650 | return Visitor->resolvedUndefsIn(F); |
| 1651 | } |
| 1652 | |
| 1653 | bool SCCPSolver::isBlockExecutable(BasicBlock *BB) const { |
| 1654 | return Visitor->isBlockExecutable(BB); |
| 1655 | } |
| 1656 | |
| 1657 | bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) const { |
| 1658 | return Visitor->isEdgeFeasible(From, To); |
| 1659 | } |
| 1660 | |
| 1661 | std::vector<ValueLatticeElement> |
| 1662 | SCCPSolver::getStructLatticeValueFor(Value *V) const { |
| 1663 | return Visitor->getStructLatticeValueFor(V); |
| 1 | Calling 'SCCPInstVisitor::getStructLatticeValueFor' | |
|
| 1664 | } |
| 1665 | |
| 1666 | void SCCPSolver::removeLatticeValueFor(Value *V) { |
| 1667 | return Visitor->removeLatticeValueFor(V); |
| 1668 | } |
| 1669 | |
| 1670 | const ValueLatticeElement &SCCPSolver::getLatticeValueFor(Value *V) const { |
| 1671 | return Visitor->getLatticeValueFor(V); |
| 1672 | } |
| 1673 | |
| 1674 | const MapVector<Function *, ValueLatticeElement> & |
| 1675 | SCCPSolver::getTrackedRetVals() { |
| 1676 | return Visitor->getTrackedRetVals(); |
| 1677 | } |
| 1678 | |
| 1679 | const DenseMap<GlobalVariable *, ValueLatticeElement> & |
| 1680 | SCCPSolver::getTrackedGlobals() { |
| 1681 | return Visitor->getTrackedGlobals(); |
| 1682 | } |
| 1683 | |
| 1684 | const SmallPtrSet<Function *, 16> SCCPSolver::getMRVFunctionsTracked() { |
| 1685 | return Visitor->getMRVFunctionsTracked(); |
| 1686 | } |
| 1687 | |
| 1688 | void SCCPSolver::markOverdefined(Value *V) { Visitor->markOverdefined(V); } |
| 1689 | |
| 1690 | bool SCCPSolver::isStructLatticeConstant(Function *F, StructType *STy) { |
| 1691 | return Visitor->isStructLatticeConstant(F, STy); |
| 1692 | } |
| 1693 | |
| 1694 | Constant *SCCPSolver::getConstant(const ValueLatticeElement &LV) const { |
| 1695 | return Visitor->getConstant(LV); |
| 1696 | } |
| 1697 | |
| 1698 | SmallPtrSetImpl<Function *> &SCCPSolver::getArgumentTrackedFunctions() { |
| 1699 | return Visitor->getArgumentTrackedFunctions(); |
| 1700 | } |
| 1701 | |
| 1702 | void SCCPSolver::markArgInFuncSpecialization(Function *F, Argument *A, |
| 1703 | Constant *C) { |
| 1704 | Visitor->markArgInFuncSpecialization(F, A, C); |
| 1705 | } |
| 1706 | |
| 1707 | void SCCPSolver::markFunctionUnreachable(Function *F) { |
| 1708 | Visitor->markFunctionUnreachable(F); |
| 1709 | } |
| 1710 | |
| 1711 | void SCCPSolver::visit(Instruction *I) { Visitor->visit(I); } |
| 1712 | |
| 1713 | void SCCPSolver::visitCall(CallInst &I) { Visitor->visitCall(I); } |