Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 1 | //===-- Lint.cpp - Check for common errors in LLVM IR ---------------------===// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass statically checks for common and easily-identified constructs |
| 11 | // which produce undefined or likely unintended behavior in LLVM IR. |
| 12 | // |
| 13 | // It is not a guarantee of correctness, in two ways. First, it isn't |
| 14 | // comprehensive. There are checks which could be done statically which are |
| 15 | // not yet implemented. Some of these are indicated by TODO comments, but |
| 16 | // those aren't comprehensive either. Second, many conditions cannot be |
| 17 | // checked statically. This pass does no dynamic instrumentation, so it |
| 18 | // can't check for all possible problems. |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 19 | // |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 20 | // Another limitation is that it assumes all code will be executed. A store |
| 21 | // through a null pointer in a basic block which is never reached is harmless, |
Dan Gohman | f855b39 | 2010-07-06 15:21:57 +0000 | [diff] [blame] | 22 | // but this pass will warn about it anyway. This is the main reason why most |
| 23 | // of these checks live here instead of in the Verifier pass. |
Dan Gohman | c951e6e | 2010-04-22 01:30:05 +0000 | [diff] [blame] | 24 | // |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 25 | // Optimization passes may make conditions that this pass checks for more or |
| 26 | // less obvious. If an optimization pass appears to be introducing a warning, |
| 27 | // it may be that the optimization pass is merely exposing an existing |
| 28 | // condition in the code. |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 29 | // |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 30 | // This code may be run before instcombine. In many cases, instcombine checks |
| 31 | // for the same kinds of things and turns instructions with undefined behavior |
| 32 | // into unreachable (or equivalent). Because of this, this pass makes some |
| 33 | // effort to look through bitcasts and so on. |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 34 | // |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/Lint.h" |
| 38 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 71f308a | 2015-02-13 09:09:03 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/AssumptionCache.h" |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ConstantFolding.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/InstructionSimplify.h" |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/Loads.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 48 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 49 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 50 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 51 | #include "llvm/IR/Function.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 52 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 53 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame^] | 54 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 55 | #include "llvm/Pass.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 56 | #include "llvm/Support/Debug.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 57 | #include "llvm/Support/raw_ostream.h" |
| 58 | using namespace llvm; |
| 59 | |
| 60 | namespace { |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 61 | namespace MemRef { |
| 62 | static unsigned Read = 1; |
| 63 | static unsigned Write = 2; |
| 64 | static unsigned Callee = 4; |
| 65 | static unsigned Branchee = 8; |
| 66 | } |
| 67 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 68 | class Lint : public FunctionPass, public InstVisitor<Lint> { |
| 69 | friend class InstVisitor<Lint>; |
| 70 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 71 | void visitFunction(Function &F); |
| 72 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 73 | void visitCallSite(CallSite CS); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 74 | void visitMemoryReference(Instruction &I, Value *Ptr, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 75 | uint64_t Size, unsigned Align, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 76 | Type *Ty, unsigned Flags); |
Andrew Kaylor | 78b53db | 2015-02-10 19:52:43 +0000 | [diff] [blame] | 77 | void visitEHBeginCatch(IntrinsicInst *II); |
| 78 | void visitEHEndCatch(IntrinsicInst *II); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 79 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 80 | void visitCallInst(CallInst &I); |
| 81 | void visitInvokeInst(InvokeInst &I); |
| 82 | void visitReturnInst(ReturnInst &I); |
| 83 | void visitLoadInst(LoadInst &I); |
| 84 | void visitStoreInst(StoreInst &I); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 85 | void visitXor(BinaryOperator &I); |
| 86 | void visitSub(BinaryOperator &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 87 | void visitLShr(BinaryOperator &I); |
| 88 | void visitAShr(BinaryOperator &I); |
| 89 | void visitShl(BinaryOperator &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 90 | void visitSDiv(BinaryOperator &I); |
| 91 | void visitUDiv(BinaryOperator &I); |
| 92 | void visitSRem(BinaryOperator &I); |
| 93 | void visitURem(BinaryOperator &I); |
| 94 | void visitAllocaInst(AllocaInst &I); |
| 95 | void visitVAArgInst(VAArgInst &I); |
| 96 | void visitIndirectBrInst(IndirectBrInst &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 97 | void visitExtractElementInst(ExtractElementInst &I); |
| 98 | void visitInsertElementInst(InsertElementInst &I); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 99 | void visitUnreachableInst(UnreachableInst &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 100 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 101 | Value *findValue(Value *V, bool OffsetOk) const; |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 102 | Value *findValueImpl(Value *V, bool OffsetOk, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 103 | SmallPtrSetImpl<Value *> &Visited) const; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 104 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 105 | public: |
| 106 | Module *Mod; |
| 107 | AliasAnalysis *AA; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 108 | AssumptionCache *AC; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 109 | DominatorTree *DT; |
Rafael Espindola | aeff8a9 | 2014-02-24 23:12:18 +0000 | [diff] [blame] | 110 | const DataLayout *DL; |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 111 | TargetLibraryInfo *TLI; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 112 | |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 113 | std::string Messages; |
| 114 | raw_string_ostream MessagesStr; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 115 | |
| 116 | static char ID; // Pass identification, replacement for typeid |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 117 | Lint() : FunctionPass(ID), MessagesStr(Messages) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 118 | initializeLintPass(*PassRegistry::getPassRegistry()); |
| 119 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 120 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 121 | bool runOnFunction(Function &F) override; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 122 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 123 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 124 | AU.setPreservesAll(); |
| 125 | AU.addRequired<AliasAnalysis>(); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 126 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 127 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 128 | AU.addRequired<DominatorTreeWrapperPass>(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 129 | } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 130 | void print(raw_ostream &O, const Module *M) const override {} |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 131 | |
| 132 | void WriteValue(const Value *V) { |
| 133 | if (!V) return; |
| 134 | if (isa<Instruction>(V)) { |
| 135 | MessagesStr << *V << '\n'; |
| 136 | } else { |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 137 | V->printAsOperand(MessagesStr, true, Mod); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 138 | MessagesStr << '\n'; |
| 139 | } |
| 140 | } |
| 141 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 142 | // CheckFailed - A check failed, so print out the condition and the message |
| 143 | // that failed. This provides a nice place to put a breakpoint if you want |
| 144 | // to see why something is not correct. |
| 145 | void CheckFailed(const Twine &Message, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 146 | const Value *V1 = nullptr, const Value *V2 = nullptr, |
| 147 | const Value *V3 = nullptr, const Value *V4 = nullptr) { |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 148 | MessagesStr << Message.str() << "\n"; |
| 149 | WriteValue(V1); |
| 150 | WriteValue(V2); |
| 151 | WriteValue(V3); |
| 152 | WriteValue(V4); |
| 153 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 154 | }; |
| 155 | } |
| 156 | |
| 157 | char Lint::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 158 | INITIALIZE_PASS_BEGIN(Lint, "lint", "Statically lint-checks LLVM IR", |
| 159 | false, true) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 160 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 161 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 162 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 163 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 164 | INITIALIZE_PASS_END(Lint, "lint", "Statically lint-checks LLVM IR", |
| 165 | false, true) |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 166 | |
| 167 | // Assert - We know that cond should be true, if not print an error message. |
| 168 | #define Assert(C, M) \ |
| 169 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
| 170 | #define Assert1(C, M, V1) \ |
| 171 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
| 172 | #define Assert2(C, M, V1, V2) \ |
| 173 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
| 174 | #define Assert3(C, M, V1, V2, V3) \ |
| 175 | do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) |
| 176 | #define Assert4(C, M, V1, V2, V3, V4) \ |
| 177 | do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) |
| 178 | |
| 179 | // Lint::run - This is the main Analysis entry point for a |
| 180 | // function. |
| 181 | // |
| 182 | bool Lint::runOnFunction(Function &F) { |
| 183 | Mod = F.getParent(); |
| 184 | AA = &getAnalysis<AliasAnalysis>(); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 185 | AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 186 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 187 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 188 | DL = DLP ? &DLP->getDataLayout() : nullptr; |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 189 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 190 | visit(F); |
| 191 | dbgs() << MessagesStr.str(); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 192 | Messages.clear(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 196 | void Lint::visitFunction(Function &F) { |
| 197 | // This isn't undefined behavior, it's just a little unusual, and it's a |
| 198 | // fairly common mistake to neglect to name a function. |
| 199 | Assert1(F.hasName() || F.hasLocalLinkage(), |
| 200 | "Unusual: Unnamed function with non-local linkage", &F); |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 201 | |
| 202 | // TODO: Check for irreducible control flow. |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void Lint::visitCallSite(CallSite CS) { |
| 206 | Instruction &I = *CS.getInstruction(); |
| 207 | Value *Callee = CS.getCalledValue(); |
| 208 | |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 209 | visitMemoryReference(I, Callee, AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 210 | 0, nullptr, MemRef::Callee); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 211 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 212 | if (Function *F = dyn_cast<Function>(findValue(Callee, /*OffsetOk=*/false))) { |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 213 | Assert1(CS.getCallingConv() == F->getCallingConv(), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 214 | "Undefined behavior: Caller and callee calling convention differ", |
| 215 | &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 217 | FunctionType *FT = F->getFunctionType(); |
Matt Arsenault | b12f2f3 | 2013-11-10 03:18:50 +0000 | [diff] [blame] | 218 | unsigned NumActualArgs = CS.arg_size(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 219 | |
| 220 | Assert1(FT->isVarArg() ? |
| 221 | FT->getNumParams() <= NumActualArgs : |
| 222 | FT->getNumParams() == NumActualArgs, |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 223 | "Undefined behavior: Call argument count mismatches callee " |
| 224 | "argument count", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 225 | |
Dan Gohman | c128e70 | 2010-07-12 18:02:04 +0000 | [diff] [blame] | 226 | Assert1(FT->getReturnType() == I.getType(), |
| 227 | "Undefined behavior: Call return type mismatches " |
| 228 | "callee return type", &I); |
| 229 | |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 230 | // Check argument types (in case the callee was casted) and attributes. |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 231 | // TODO: Verify that caller and callee attributes are compatible. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 232 | Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end(); |
| 233 | CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 234 | for (; AI != AE; ++AI) { |
| 235 | Value *Actual = *AI; |
| 236 | if (PI != PE) { |
| 237 | Argument *Formal = PI++; |
| 238 | Assert1(Formal->getType() == Actual->getType(), |
| 239 | "Undefined behavior: Call argument type mismatches " |
| 240 | "callee parameter type", &I); |
Dan Gohman | 49a372c | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 241 | |
Dan Gohman | 3cb55a1 | 2010-12-13 22:53:18 +0000 | [diff] [blame] | 242 | // Check that noalias arguments don't alias other arguments. This is |
| 243 | // not fully precise because we don't know the sizes of the dereferenced |
| 244 | // memory regions. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 245 | if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy()) |
Dan Gohman | 7dacf8f | 2010-11-11 19:23:51 +0000 | [diff] [blame] | 246 | for (CallSite::arg_iterator BI = CS.arg_begin(); BI != AE; ++BI) |
Dan Gohman | 201acdb | 2010-12-10 20:04:06 +0000 | [diff] [blame] | 247 | if (AI != BI && (*BI)->getType()->isPointerTy()) { |
| 248 | AliasAnalysis::AliasResult Result = AA->alias(*AI, *BI); |
| 249 | Assert1(Result != AliasAnalysis::MustAlias && |
| 250 | Result != AliasAnalysis::PartialAlias, |
| 251 | "Unusual: noalias argument aliases another argument", &I); |
| 252 | } |
Dan Gohman | 49a372c | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 253 | |
| 254 | // Check that an sret argument points to valid memory. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 255 | if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 256 | Type *Ty = |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 257 | cast<PointerType>(Formal->getType())->getElementType(); |
| 258 | visitMemoryReference(I, Actual, AA->getTypeStoreSize(Ty), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 259 | DL ? DL->getABITypeAlignment(Ty) : 0, |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 260 | Ty, MemRef::Read | MemRef::Write); |
| 261 | } |
| 262 | } |
| 263 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Dan Gohman | 1249adf | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 266 | if (CS.isCall() && cast<CallInst>(CS.getInstruction())->isTailCall()) |
| 267 | for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 268 | AI != AE; ++AI) { |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 269 | Value *Obj = findValue(*AI, /*OffsetOk=*/true); |
Dan Gohman | cef9fc3 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 270 | Assert1(!isa<AllocaInst>(Obj), |
Dan Gohman | 1249adf | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 271 | "Undefined behavior: Call with \"tail\" keyword references " |
Dan Gohman | cef9fc3 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 272 | "alloca", &I); |
Dan Gohman | 1249adf | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 275 | |
| 276 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) |
| 277 | switch (II->getIntrinsicID()) { |
| 278 | default: break; |
| 279 | |
| 280 | // TODO: Check more intrinsics |
| 281 | |
| 282 | case Intrinsic::memcpy: { |
| 283 | MemCpyInst *MCI = cast<MemCpyInst>(&I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 284 | // TODO: If the size is known, use it. |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 285 | visitMemoryReference(I, MCI->getDest(), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 286 | MCI->getAlignment(), nullptr, |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 287 | MemRef::Write); |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 288 | visitMemoryReference(I, MCI->getSource(), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 289 | MCI->getAlignment(), nullptr, |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 290 | MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 291 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 292 | // Check that the memcpy arguments don't overlap. The AliasAnalysis API |
| 293 | // isn't expressive enough for what we really want to do. Known partial |
| 294 | // overlap is not distinguished from the case where nothing is known. |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 295 | uint64_t Size = 0; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 296 | if (const ConstantInt *Len = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 297 | dyn_cast<ConstantInt>(findValue(MCI->getLength(), |
| 298 | /*OffsetOk=*/false))) |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 299 | if (Len->getValue().isIntN(32)) |
| 300 | Size = Len->getValue().getZExtValue(); |
| 301 | Assert1(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) != |
| 302 | AliasAnalysis::MustAlias, |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 303 | "Undefined behavior: memcpy source and destination overlap", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | case Intrinsic::memmove: { |
| 307 | MemMoveInst *MMI = cast<MemMoveInst>(&I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 308 | // TODO: If the size is known, use it. |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 309 | visitMemoryReference(I, MMI->getDest(), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 310 | MMI->getAlignment(), nullptr, |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 311 | MemRef::Write); |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 312 | visitMemoryReference(I, MMI->getSource(), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 313 | MMI->getAlignment(), nullptr, |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 314 | MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | case Intrinsic::memset: { |
| 318 | MemSetInst *MSI = cast<MemSetInst>(&I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 319 | // TODO: If the size is known, use it. |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 320 | visitMemoryReference(I, MSI->getDest(), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 321 | MSI->getAlignment(), nullptr, |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 322 | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | |
| 326 | case Intrinsic::vastart: |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 327 | Assert1(I.getParent()->getParent()->isVarArg(), |
| 328 | "Undefined behavior: va_start called in a non-varargs function", |
| 329 | &I); |
| 330 | |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 331 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 332 | 0, nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 333 | break; |
| 334 | case Intrinsic::vacopy: |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 335 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 336 | 0, nullptr, MemRef::Write); |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 337 | visitMemoryReference(I, CS.getArgument(1), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 338 | 0, nullptr, MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 339 | break; |
| 340 | case Intrinsic::vaend: |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 341 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 342 | 0, nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 343 | break; |
Dan Gohman | a20a5cd | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 344 | |
| 345 | case Intrinsic::stackrestore: |
| 346 | // Stackrestore doesn't read or write memory, but it sets the |
| 347 | // stack pointer, which the compiler may read from or write to |
| 348 | // at any time, so check it for both readability and writeability. |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 349 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 350 | 0, nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | a20a5cd | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 351 | break; |
Andrew Kaylor | 78b53db | 2015-02-10 19:52:43 +0000 | [diff] [blame] | 352 | |
| 353 | case Intrinsic::eh_begincatch: |
| 354 | visitEHBeginCatch(II); |
| 355 | break; |
| 356 | case Intrinsic::eh_endcatch: |
| 357 | visitEHEndCatch(II); |
| 358 | break; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | void Lint::visitCallInst(CallInst &I) { |
| 363 | return visitCallSite(&I); |
| 364 | } |
| 365 | |
| 366 | void Lint::visitInvokeInst(InvokeInst &I) { |
| 367 | return visitCallSite(&I); |
| 368 | } |
| 369 | |
| 370 | void Lint::visitReturnInst(ReturnInst &I) { |
| 371 | Function *F = I.getParent()->getParent(); |
| 372 | Assert1(!F->doesNotReturn(), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 373 | "Unusual: Return statement in function with noreturn attribute", |
| 374 | &I); |
Dan Gohman | ddba4b7 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 375 | |
| 376 | if (Value *V = I.getReturnValue()) { |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 377 | Value *Obj = findValue(V, /*OffsetOk=*/true); |
Dan Gohman | cef9fc3 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 378 | Assert1(!isa<AllocaInst>(Obj), |
| 379 | "Unusual: Returning alloca value", &I); |
Dan Gohman | ddba4b7 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 380 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 383 | // TODO: Check that the reference is in bounds. |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 384 | // TODO: Check readnone/readonly function attributes. |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 385 | void Lint::visitMemoryReference(Instruction &I, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 386 | Value *Ptr, uint64_t Size, unsigned Align, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 387 | Type *Ty, unsigned Flags) { |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 388 | // If no memory is being referenced, it doesn't matter if the pointer |
| 389 | // is valid. |
| 390 | if (Size == 0) |
| 391 | return; |
| 392 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 393 | Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 394 | Assert1(!isa<ConstantPointerNull>(UnderlyingObject), |
| 395 | "Undefined behavior: Null pointer dereference", &I); |
| 396 | Assert1(!isa<UndefValue>(UnderlyingObject), |
| 397 | "Undefined behavior: Undef pointer dereference", &I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 398 | Assert1(!isa<ConstantInt>(UnderlyingObject) || |
| 399 | !cast<ConstantInt>(UnderlyingObject)->isAllOnesValue(), |
| 400 | "Unusual: All-ones pointer dereference", &I); |
| 401 | Assert1(!isa<ConstantInt>(UnderlyingObject) || |
| 402 | !cast<ConstantInt>(UnderlyingObject)->isOne(), |
| 403 | "Unusual: Address one pointer dereference", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 404 | |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 405 | if (Flags & MemRef::Write) { |
| 406 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject)) |
| 407 | Assert1(!GV->isConstant(), |
| 408 | "Undefined behavior: Write to read-only memory", &I); |
| 409 | Assert1(!isa<Function>(UnderlyingObject) && |
| 410 | !isa<BlockAddress>(UnderlyingObject), |
| 411 | "Undefined behavior: Write to text section", &I); |
| 412 | } |
| 413 | if (Flags & MemRef::Read) { |
| 414 | Assert1(!isa<Function>(UnderlyingObject), |
| 415 | "Unusual: Load from function body", &I); |
| 416 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 417 | "Undefined behavior: Load from block address", &I); |
| 418 | } |
| 419 | if (Flags & MemRef::Callee) { |
| 420 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 421 | "Undefined behavior: Call to block address", &I); |
| 422 | } |
| 423 | if (Flags & MemRef::Branchee) { |
| 424 | Assert1(!isa<Constant>(UnderlyingObject) || |
| 425 | isa<BlockAddress>(UnderlyingObject), |
| 426 | "Undefined behavior: Branch to non-blockaddress", &I); |
| 427 | } |
| 428 | |
Duncan Sands | a221eea | 2012-09-26 07:45:36 +0000 | [diff] [blame] | 429 | // Check for buffer overflows and misalignment. |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 430 | // Only handles memory references that read/write something simple like an |
| 431 | // alloca instruction or a global variable. |
| 432 | int64_t Offset = 0; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 433 | if (Value *Base = GetPointerBaseWithConstantOffset(Ptr, Offset, DL)) { |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 434 | // OK, so the access is to a constant offset from Ptr. Check that Ptr is |
| 435 | // something we can handle and if so extract the size of this base object |
| 436 | // along with its alignment. |
| 437 | uint64_t BaseSize = AliasAnalysis::UnknownSize; |
| 438 | unsigned BaseAlign = 0; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 439 | |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 440 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Base)) { |
| 441 | Type *ATy = AI->getAllocatedType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 442 | if (DL && !AI->isArrayAllocation() && ATy->isSized()) |
| 443 | BaseSize = DL->getTypeAllocSize(ATy); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 444 | BaseAlign = AI->getAlignment(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 445 | if (DL && BaseAlign == 0 && ATy->isSized()) |
| 446 | BaseAlign = DL->getABITypeAlignment(ATy); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 447 | } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) { |
| 448 | // If the global may be defined differently in another compilation unit |
| 449 | // then don't warn about funky memory accesses. |
| 450 | if (GV->hasDefinitiveInitializer()) { |
| 451 | Type *GTy = GV->getType()->getElementType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 452 | if (DL && GTy->isSized()) |
| 453 | BaseSize = DL->getTypeAllocSize(GTy); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 454 | BaseAlign = GV->getAlignment(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 455 | if (DL && BaseAlign == 0 && GTy->isSized()) |
| 456 | BaseAlign = DL->getABITypeAlignment(GTy); |
Duncan Sands | 3f4d0b1 | 2012-09-25 10:00:49 +0000 | [diff] [blame] | 457 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 458 | } |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 459 | |
| 460 | // Accesses from before the start or after the end of the object are not |
| 461 | // defined. |
| 462 | Assert1(Size == AliasAnalysis::UnknownSize || |
| 463 | BaseSize == AliasAnalysis::UnknownSize || |
| 464 | (Offset >= 0 && Offset + Size <= BaseSize), |
| 465 | "Undefined behavior: Buffer overflow", &I); |
| 466 | |
| 467 | // Accesses that say that the memory is more aligned than it is are not |
| 468 | // defined. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 469 | if (DL && Align == 0 && Ty && Ty->isSized()) |
| 470 | Align = DL->getABITypeAlignment(Ty); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 471 | Assert1(!BaseAlign || Align <= MinAlign(BaseAlign, Offset), |
| 472 | "Undefined behavior: Memory reference address is misaligned", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | void Lint::visitLoadInst(LoadInst &I) { |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 477 | visitMemoryReference(I, I.getPointerOperand(), |
| 478 | AA->getTypeStoreSize(I.getType()), I.getAlignment(), |
| 479 | I.getType(), MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | void Lint::visitStoreInst(StoreInst &I) { |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 483 | visitMemoryReference(I, I.getPointerOperand(), |
| 484 | AA->getTypeStoreSize(I.getOperand(0)->getType()), |
| 485 | I.getAlignment(), |
| 486 | I.getOperand(0)->getType(), MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 489 | void Lint::visitXor(BinaryOperator &I) { |
| 490 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 491 | !isa<UndefValue>(I.getOperand(1)), |
| 492 | "Undefined result: xor(undef, undef)", &I); |
| 493 | } |
| 494 | |
| 495 | void Lint::visitSub(BinaryOperator &I) { |
| 496 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 497 | !isa<UndefValue>(I.getOperand(1)), |
| 498 | "Undefined result: sub(undef, undef)", &I); |
| 499 | } |
| 500 | |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 501 | void Lint::visitLShr(BinaryOperator &I) { |
| 502 | if (ConstantInt *CI = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 503 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 504 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 505 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void Lint::visitAShr(BinaryOperator &I) { |
| 509 | if (ConstantInt *CI = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 510 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 511 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 512 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void Lint::visitShl(BinaryOperator &I) { |
| 516 | if (ConstantInt *CI = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 517 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 518 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 519 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Andrew Kaylor | 78b53db | 2015-02-10 19:52:43 +0000 | [diff] [blame] | 522 | static bool |
| 523 | allPredsCameFromLandingPad(BasicBlock *BB, |
| 524 | SmallSet<BasicBlock *, 4> &VisitedBlocks) { |
| 525 | VisitedBlocks.insert(BB); |
| 526 | if (BB->isLandingPad()) |
| 527 | return true; |
| 528 | // If we find a block with no predecessors, the search failed. |
| 529 | if (pred_empty(BB)) |
| 530 | return false; |
| 531 | for (BasicBlock *Pred : predecessors(BB)) { |
| 532 | if (VisitedBlocks.count(Pred)) |
| 533 | continue; |
| 534 | if (!allPredsCameFromLandingPad(Pred, VisitedBlocks)) |
| 535 | return false; |
| 536 | } |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | static bool |
| 541 | allSuccessorsReachEndCatch(BasicBlock *BB, BasicBlock::iterator InstBegin, |
| 542 | IntrinsicInst **SecondBeginCatch, |
| 543 | SmallSet<BasicBlock *, 4> &VisitedBlocks) { |
| 544 | VisitedBlocks.insert(BB); |
| 545 | for (BasicBlock::iterator I = InstBegin, E = BB->end(); I != E; ++I) { |
| 546 | IntrinsicInst *IC = dyn_cast<IntrinsicInst>(I); |
| 547 | if (IC && IC->getIntrinsicID() == Intrinsic::eh_endcatch) |
| 548 | return true; |
| 549 | // If we find another begincatch while looking for an endcatch, |
| 550 | // that's also an error. |
| 551 | if (IC && IC->getIntrinsicID() == Intrinsic::eh_begincatch) { |
| 552 | *SecondBeginCatch = IC; |
| 553 | return false; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | // If we reach a block with no successors while searching, the |
| 558 | // search has failed. |
| 559 | if (succ_empty(BB)) |
| 560 | return false; |
| 561 | // Otherwise, search all of the successors. |
| 562 | for (BasicBlock *Succ : successors(BB)) { |
| 563 | if (VisitedBlocks.count(Succ)) |
| 564 | continue; |
| 565 | if (!allSuccessorsReachEndCatch(Succ, Succ->begin(), SecondBeginCatch, |
| 566 | VisitedBlocks)) |
| 567 | return false; |
| 568 | } |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | void Lint::visitEHBeginCatch(IntrinsicInst *II) { |
| 573 | // The checks in this function make a potentially dubious assumption about |
| 574 | // the CFG, namely that any block involved in a catch is only used for the |
| 575 | // catch. This will very likely be true of IR generated by a front end, |
| 576 | // but it may cease to be true, for example, if the IR is run through a |
| 577 | // pass which combines similar blocks. |
| 578 | // |
| 579 | // In general, if we encounter a block the isn't dominated by the catch |
| 580 | // block while we are searching the catch block's successors for a call |
| 581 | // to end catch intrinsic, then it is possible that it will be legal for |
| 582 | // a path through this block to never reach a call to llvm.eh.endcatch. |
| 583 | // An analogous statement could be made about our search for a landing |
| 584 | // pad among the catch block's predecessors. |
| 585 | // |
| 586 | // What is actually required is that no path is possible at runtime that |
| 587 | // reaches a call to llvm.eh.begincatch without having previously visited |
| 588 | // a landingpad instruction and that no path is possible at runtime that |
| 589 | // calls llvm.eh.begincatch and does not subsequently call llvm.eh.endcatch |
| 590 | // (mentally adjusting for the fact that in reality these calls will be |
| 591 | // removed before code generation). |
| 592 | // |
| 593 | // Because this is a lint check, we take a pessimistic approach and warn if |
| 594 | // the control flow is potentially incorrect. |
| 595 | |
| 596 | SmallSet<BasicBlock *, 4> VisitedBlocks; |
| 597 | BasicBlock *CatchBB = II->getParent(); |
| 598 | |
| 599 | // The begin catch must occur in a landing pad block or all paths |
| 600 | // to it must have come from a landing pad. |
| 601 | Assert1(allPredsCameFromLandingPad(CatchBB, VisitedBlocks), |
| 602 | "llvm.eh.begincatch may be reachable without passing a landingpad", |
| 603 | II); |
| 604 | |
| 605 | // Reset the visited block list. |
| 606 | VisitedBlocks.clear(); |
| 607 | |
| 608 | IntrinsicInst *SecondBeginCatch = nullptr; |
| 609 | |
| 610 | // This has to be called before it is asserted. Otherwise, the first assert |
| 611 | // below can never be hit. |
| 612 | bool EndCatchFound = allSuccessorsReachEndCatch( |
| 613 | CatchBB, std::next(static_cast<BasicBlock::iterator>(II)), |
| 614 | &SecondBeginCatch, VisitedBlocks); |
| 615 | Assert2( |
| 616 | SecondBeginCatch == nullptr, |
| 617 | "llvm.eh.begincatch may be called a second time before llvm.eh.endcatch", |
| 618 | II, SecondBeginCatch); |
| 619 | Assert1(EndCatchFound, |
| 620 | "Some paths from llvm.eh.begincatch may not reach llvm.eh.endcatch", |
| 621 | II); |
| 622 | } |
| 623 | |
| 624 | static bool allPredCameFromBeginCatch( |
| 625 | BasicBlock *BB, BasicBlock::reverse_iterator InstRbegin, |
| 626 | IntrinsicInst **SecondEndCatch, SmallSet<BasicBlock *, 4> &VisitedBlocks) { |
| 627 | VisitedBlocks.insert(BB); |
| 628 | // Look for a begincatch in this block. |
| 629 | for (BasicBlock::reverse_iterator RI = InstRbegin, RE = BB->rend(); RI != RE; |
| 630 | ++RI) { |
| 631 | IntrinsicInst *IC = dyn_cast<IntrinsicInst>(&*RI); |
| 632 | if (IC && IC->getIntrinsicID() == Intrinsic::eh_begincatch) |
| 633 | return true; |
| 634 | // If we find another end catch before we find a begin catch, that's |
| 635 | // an error. |
| 636 | if (IC && IC->getIntrinsicID() == Intrinsic::eh_endcatch) { |
| 637 | *SecondEndCatch = IC; |
| 638 | return false; |
| 639 | } |
| 640 | // If we encounter a landingpad instruction, the search failed. |
| 641 | if (isa<LandingPadInst>(*RI)) |
| 642 | return false; |
| 643 | } |
| 644 | // If while searching we find a block with no predeccesors, |
| 645 | // the search failed. |
| 646 | if (pred_empty(BB)) |
| 647 | return false; |
| 648 | // Search any predecessors we haven't seen before. |
| 649 | for (BasicBlock *Pred : predecessors(BB)) { |
| 650 | if (VisitedBlocks.count(Pred)) |
| 651 | continue; |
| 652 | if (!allPredCameFromBeginCatch(Pred, Pred->rbegin(), SecondEndCatch, |
| 653 | VisitedBlocks)) |
| 654 | return false; |
| 655 | } |
| 656 | return true; |
| 657 | } |
| 658 | |
| 659 | void Lint::visitEHEndCatch(IntrinsicInst *II) { |
| 660 | // The check in this function makes a potentially dubious assumption about |
| 661 | // the CFG, namely that any block involved in a catch is only used for the |
| 662 | // catch. This will very likely be true of IR generated by a front end, |
| 663 | // but it may cease to be true, for example, if the IR is run through a |
| 664 | // pass which combines similar blocks. |
| 665 | // |
| 666 | // In general, if we encounter a block the isn't post-dominated by the |
| 667 | // end catch block while we are searching the end catch block's predecessors |
| 668 | // for a call to the begin catch intrinsic, then it is possible that it will |
| 669 | // be legal for a path to reach the end catch block without ever having |
| 670 | // called llvm.eh.begincatch. |
| 671 | // |
| 672 | // What is actually required is that no path is possible at runtime that |
| 673 | // reaches a call to llvm.eh.endcatch without having previously visited |
| 674 | // a call to llvm.eh.begincatch (mentally adjusting for the fact that in |
| 675 | // reality these calls will be removed before code generation). |
| 676 | // |
| 677 | // Because this is a lint check, we take a pessimistic approach and warn if |
| 678 | // the control flow is potentially incorrect. |
| 679 | |
| 680 | BasicBlock *EndCatchBB = II->getParent(); |
| 681 | |
| 682 | // Alls paths to the end catch call must pass through a begin catch call. |
| 683 | |
| 684 | // If llvm.eh.begincatch wasn't called in the current block, we'll use this |
| 685 | // lambda to recursively look for it in predecessors. |
| 686 | SmallSet<BasicBlock *, 4> VisitedBlocks; |
| 687 | IntrinsicInst *SecondEndCatch = nullptr; |
| 688 | |
| 689 | // This has to be called before it is asserted. Otherwise, the first assert |
| 690 | // below can never be hit. |
| 691 | bool BeginCatchFound = |
| 692 | allPredCameFromBeginCatch(EndCatchBB, BasicBlock::reverse_iterator(II), |
| 693 | &SecondEndCatch, VisitedBlocks); |
| 694 | Assert2( |
| 695 | SecondEndCatch == nullptr, |
| 696 | "llvm.eh.endcatch may be called a second time after llvm.eh.begincatch", |
| 697 | II, SecondEndCatch); |
| 698 | Assert1( |
| 699 | BeginCatchFound, |
| 700 | "llvm.eh.endcatch may be reachable without passing llvm.eh.begincatch", |
| 701 | II); |
| 702 | } |
| 703 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 704 | static bool isZero(Value *V, const DataLayout *DL, DominatorTree *DT, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 705 | AssumptionCache *AC) { |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 706 | // Assume undef could be zero. |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 707 | if (isa<UndefValue>(V)) |
| 708 | return true; |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 709 | |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 710 | VectorType *VecTy = dyn_cast<VectorType>(V->getType()); |
| 711 | if (!VecTy) { |
| 712 | unsigned BitWidth = V->getType()->getIntegerBitWidth(); |
| 713 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 714 | computeKnownBits(V, KnownZero, KnownOne, DL, 0, AC, |
| 715 | dyn_cast<Instruction>(V), DT); |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 716 | return KnownZero.isAllOnesValue(); |
| 717 | } |
| 718 | |
| 719 | // Per-component check doesn't work with zeroinitializer |
| 720 | Constant *C = dyn_cast<Constant>(V); |
| 721 | if (!C) |
| 722 | return false; |
| 723 | |
| 724 | if (C->isZeroValue()) |
| 725 | return true; |
| 726 | |
| 727 | // For a vector, KnownZero will only be true if all values are zero, so check |
| 728 | // this per component |
| 729 | unsigned BitWidth = VecTy->getElementType()->getIntegerBitWidth(); |
| 730 | for (unsigned I = 0, N = VecTy->getNumElements(); I != N; ++I) { |
| 731 | Constant *Elem = C->getAggregateElement(I); |
| 732 | if (isa<UndefValue>(Elem)) |
| 733 | return true; |
| 734 | |
| 735 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 736 | computeKnownBits(Elem, KnownZero, KnownOne, DL); |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 737 | if (KnownZero.isAllOnesValue()) |
| 738 | return true; |
| 739 | } |
| 740 | |
| 741 | return false; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | void Lint::visitSDiv(BinaryOperator &I) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 745 | Assert1(!isZero(I.getOperand(1), DL, DT, AC), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 746 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | void Lint::visitUDiv(BinaryOperator &I) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 750 | Assert1(!isZero(I.getOperand(1), DL, DT, AC), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 751 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | void Lint::visitSRem(BinaryOperator &I) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 755 | Assert1(!isZero(I.getOperand(1), DL, DT, AC), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 756 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | void Lint::visitURem(BinaryOperator &I) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 760 | Assert1(!isZero(I.getOperand(1), DL, DT, AC), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 761 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | void Lint::visitAllocaInst(AllocaInst &I) { |
| 765 | if (isa<ConstantInt>(I.getArraySize())) |
| 766 | // This isn't undefined behavior, it's just an obvious pessimization. |
| 767 | Assert1(&I.getParent()->getParent()->getEntryBlock() == I.getParent(), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 768 | "Pessimization: Static alloca outside of entry block", &I); |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 769 | |
| 770 | // TODO: Check for an unusual size (MSB set?) |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | void Lint::visitVAArgInst(VAArgInst &I) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 774 | visitMemoryReference(I, I.getOperand(0), AliasAnalysis::UnknownSize, 0, |
| 775 | nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | void Lint::visitIndirectBrInst(IndirectBrInst &I) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 779 | visitMemoryReference(I, I.getAddress(), AliasAnalysis::UnknownSize, 0, |
| 780 | nullptr, MemRef::Branchee); |
Dan Gohman | d8968da | 2010-08-02 23:06:43 +0000 | [diff] [blame] | 781 | |
| 782 | Assert1(I.getNumDestinations() != 0, |
| 783 | "Undefined behavior: indirectbr with no destinations", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 786 | void Lint::visitExtractElementInst(ExtractElementInst &I) { |
| 787 | if (ConstantInt *CI = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 788 | dyn_cast<ConstantInt>(findValue(I.getIndexOperand(), |
| 789 | /*OffsetOk=*/false))) |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 790 | Assert1(CI->getValue().ult(I.getVectorOperandType()->getNumElements()), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 791 | "Undefined result: extractelement index out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | void Lint::visitInsertElementInst(InsertElementInst &I) { |
| 795 | if (ConstantInt *CI = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 796 | dyn_cast<ConstantInt>(findValue(I.getOperand(2), |
| 797 | /*OffsetOk=*/false))) |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 798 | Assert1(CI->getValue().ult(I.getType()->getNumElements()), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 799 | "Undefined result: insertelement index out of range", &I); |
| 800 | } |
| 801 | |
| 802 | void Lint::visitUnreachableInst(UnreachableInst &I) { |
| 803 | // This isn't undefined behavior, it's merely suspicious. |
| 804 | Assert1(&I == I.getParent()->begin() || |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 805 | std::prev(BasicBlock::iterator(&I))->mayHaveSideEffects(), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 806 | "Unusual: unreachable immediately preceded by instruction without " |
| 807 | "side effects", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 810 | /// findValue - Look through bitcasts and simple memory reference patterns |
| 811 | /// to identify an equivalent, but more informative, value. If OffsetOk |
| 812 | /// is true, look through getelementptrs with non-zero offsets too. |
| 813 | /// |
| 814 | /// Most analysis passes don't require this logic, because instcombine |
| 815 | /// will simplify most of these kinds of things away. But it's a goal of |
| 816 | /// this Lint pass to be useful even on non-optimized IR. |
| 817 | Value *Lint::findValue(Value *V, bool OffsetOk) const { |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 818 | SmallPtrSet<Value *, 4> Visited; |
| 819 | return findValueImpl(V, OffsetOk, Visited); |
| 820 | } |
| 821 | |
| 822 | /// findValueImpl - Implementation helper for findValue. |
| 823 | Value *Lint::findValueImpl(Value *V, bool OffsetOk, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 824 | SmallPtrSetImpl<Value *> &Visited) const { |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 825 | // Detect self-referential values. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 826 | if (!Visited.insert(V).second) |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 827 | return UndefValue::get(V->getType()); |
| 828 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 829 | // TODO: Look through sext or zext cast, when the result is known to |
| 830 | // be interpreted as signed or unsigned, respectively. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 831 | // TODO: Look through eliminable cast pairs. |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 832 | // TODO: Look through calls with unique return values. |
| 833 | // TODO: Look through vector insert/extract/shuffle. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 834 | V = OffsetOk ? GetUnderlyingObject(V, DL) : V->stripPointerCasts(); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 835 | if (LoadInst *L = dyn_cast<LoadInst>(V)) { |
| 836 | BasicBlock::iterator BBI = L; |
| 837 | BasicBlock *BB = L->getParent(); |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 838 | SmallPtrSet<BasicBlock *, 4> VisitedBlocks; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 839 | for (;;) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 840 | if (!VisitedBlocks.insert(BB).second) |
| 841 | break; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 842 | if (Value *U = FindAvailableLoadedValue(L->getPointerOperand(), |
| 843 | BB, BBI, 6, AA)) |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 844 | return findValueImpl(U, OffsetOk, Visited); |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 845 | if (BBI != BB->begin()) break; |
| 846 | BB = BB->getUniquePredecessor(); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 847 | if (!BB) break; |
| 848 | BBI = BB->end(); |
| 849 | } |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 850 | } else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 851 | if (Value *W = PN->hasConstantValue()) |
Duncan Sands | ec7a6ec | 2010-11-17 10:23:23 +0000 | [diff] [blame] | 852 | if (W != V) |
| 853 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 854 | } else if (CastInst *CI = dyn_cast<CastInst>(V)) { |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 855 | if (CI->isNoopCast(DL)) |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 856 | return findValueImpl(CI->getOperand(0), OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 857 | } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) { |
| 858 | if (Value *W = FindInsertedValue(Ex->getAggregateOperand(), |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 859 | Ex->getIndices())) |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 860 | if (W != V) |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 861 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 862 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 863 | // Same as above, but for ConstantExpr instead of Instruction. |
| 864 | if (Instruction::isCast(CE->getOpcode())) { |
| 865 | if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()), |
| 866 | CE->getOperand(0)->getType(), |
| 867 | CE->getType(), |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 868 | DL ? DL->getIntPtrType(V->getType()) : |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 869 | Type::getInt64Ty(V->getContext()))) |
| 870 | return findValueImpl(CE->getOperand(0), OffsetOk, Visited); |
| 871 | } else if (CE->getOpcode() == Instruction::ExtractValue) { |
Jay Foad | 0091fe8 | 2011-04-13 15:22:40 +0000 | [diff] [blame] | 872 | ArrayRef<unsigned> Indices = CE->getIndices(); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 873 | if (Value *W = FindInsertedValue(CE->getOperand(0), Indices)) |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 874 | if (W != V) |
| 875 | return findValueImpl(W, OffsetOk, Visited); |
| 876 | } |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | // As a last resort, try SimplifyInstruction or constant folding. |
| 880 | if (Instruction *Inst = dyn_cast<Instruction>(V)) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 881 | if (Value *W = SimplifyInstruction(Inst, DL, TLI, DT, AC)) |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 882 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 883 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 884 | if (Value *W = ConstantFoldConstantExpression(CE, DL, TLI)) |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 885 | if (W != V) |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 886 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | return V; |
| 890 | } |
| 891 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 892 | //===----------------------------------------------------------------------===// |
| 893 | // Implement the public interfaces to this file... |
| 894 | //===----------------------------------------------------------------------===// |
| 895 | |
| 896 | FunctionPass *llvm::createLintPass() { |
| 897 | return new Lint(); |
| 898 | } |
| 899 | |
| 900 | /// lintFunction - Check a function for errors, printing messages on stderr. |
| 901 | /// |
| 902 | void llvm::lintFunction(const Function &f) { |
| 903 | Function &F = const_cast<Function&>(f); |
| 904 | assert(!F.isDeclaration() && "Cannot lint external functions"); |
| 905 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame^] | 906 | legacy::FunctionPassManager FPM(F.getParent()); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 907 | Lint *V = new Lint(); |
| 908 | FPM.add(V); |
| 909 | FPM.run(F); |
| 910 | } |
| 911 | |
| 912 | /// lintModule - Check a module for errors, printing messages on stderr. |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 913 | /// |
Dan Gohman | 084bcb1 | 2010-05-26 22:28:53 +0000 | [diff] [blame] | 914 | void llvm::lintModule(const Module &M) { |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame^] | 915 | legacy::PassManager PM; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 916 | Lint *V = new Lint(); |
| 917 | PM.add(V); |
| 918 | PM.run(const_cast<Module&>(M)); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 919 | } |