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" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/AliasAnalysis.h" |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 40 | #include "llvm/Analysis/AssumptionTracker.h" |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/ConstantFolding.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/InstructionSimplify.h" |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/Loads.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/Passes.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 46 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 47 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 48 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 49 | #include "llvm/IR/Function.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 50 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 51 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 52 | #include "llvm/Pass.h" |
| 53 | #include "llvm/PassManager.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Debug.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 55 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 56 | #include "llvm/Target/TargetLibraryInfo.h" |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 57 | using namespace llvm; |
| 58 | |
| 59 | namespace { |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 60 | namespace MemRef { |
| 61 | static unsigned Read = 1; |
| 62 | static unsigned Write = 2; |
| 63 | static unsigned Callee = 4; |
| 64 | static unsigned Branchee = 8; |
| 65 | } |
| 66 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 67 | class Lint : public FunctionPass, public InstVisitor<Lint> { |
| 68 | friend class InstVisitor<Lint>; |
| 69 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 70 | void visitFunction(Function &F); |
| 71 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 72 | void visitCallSite(CallSite CS); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 73 | void visitMemoryReference(Instruction &I, Value *Ptr, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 74 | uint64_t Size, unsigned Align, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 75 | Type *Ty, unsigned Flags); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 76 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 77 | void visitCallInst(CallInst &I); |
| 78 | void visitInvokeInst(InvokeInst &I); |
| 79 | void visitReturnInst(ReturnInst &I); |
| 80 | void visitLoadInst(LoadInst &I); |
| 81 | void visitStoreInst(StoreInst &I); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 82 | void visitXor(BinaryOperator &I); |
| 83 | void visitSub(BinaryOperator &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 84 | void visitLShr(BinaryOperator &I); |
| 85 | void visitAShr(BinaryOperator &I); |
| 86 | void visitShl(BinaryOperator &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 87 | void visitSDiv(BinaryOperator &I); |
| 88 | void visitUDiv(BinaryOperator &I); |
| 89 | void visitSRem(BinaryOperator &I); |
| 90 | void visitURem(BinaryOperator &I); |
| 91 | void visitAllocaInst(AllocaInst &I); |
| 92 | void visitVAArgInst(VAArgInst &I); |
| 93 | void visitIndirectBrInst(IndirectBrInst &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 94 | void visitExtractElementInst(ExtractElementInst &I); |
| 95 | void visitInsertElementInst(InsertElementInst &I); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 96 | void visitUnreachableInst(UnreachableInst &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 97 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 98 | Value *findValue(Value *V, bool OffsetOk) const; |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 99 | Value *findValueImpl(Value *V, bool OffsetOk, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 100 | SmallPtrSetImpl<Value *> &Visited) const; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 101 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 102 | public: |
| 103 | Module *Mod; |
| 104 | AliasAnalysis *AA; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 105 | AssumptionTracker *AT; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 106 | DominatorTree *DT; |
Rafael Espindola | aeff8a9 | 2014-02-24 23:12:18 +0000 | [diff] [blame] | 107 | const DataLayout *DL; |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 108 | TargetLibraryInfo *TLI; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 109 | |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 110 | std::string Messages; |
| 111 | raw_string_ostream MessagesStr; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 112 | |
| 113 | static char ID; // Pass identification, replacement for typeid |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 114 | Lint() : FunctionPass(ID), MessagesStr(Messages) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 115 | initializeLintPass(*PassRegistry::getPassRegistry()); |
| 116 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 117 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 118 | bool runOnFunction(Function &F) override; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 119 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 120 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 121 | AU.setPreservesAll(); |
| 122 | AU.addRequired<AliasAnalysis>(); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 123 | AU.addRequired<AssumptionTracker>(); |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 124 | AU.addRequired<TargetLibraryInfo>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 125 | AU.addRequired<DominatorTreeWrapperPass>(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 126 | } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 127 | void print(raw_ostream &O, const Module *M) const override {} |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 128 | |
| 129 | void WriteValue(const Value *V) { |
| 130 | if (!V) return; |
| 131 | if (isa<Instruction>(V)) { |
| 132 | MessagesStr << *V << '\n'; |
| 133 | } else { |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 134 | V->printAsOperand(MessagesStr, true, Mod); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 135 | MessagesStr << '\n'; |
| 136 | } |
| 137 | } |
| 138 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 139 | // CheckFailed - A check failed, so print out the condition and the message |
| 140 | // that failed. This provides a nice place to put a breakpoint if you want |
| 141 | // to see why something is not correct. |
| 142 | void CheckFailed(const Twine &Message, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 143 | const Value *V1 = nullptr, const Value *V2 = nullptr, |
| 144 | const Value *V3 = nullptr, const Value *V4 = nullptr) { |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 145 | MessagesStr << Message.str() << "\n"; |
| 146 | WriteValue(V1); |
| 147 | WriteValue(V2); |
| 148 | WriteValue(V3); |
| 149 | WriteValue(V4); |
| 150 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 151 | }; |
| 152 | } |
| 153 | |
| 154 | char Lint::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 155 | INITIALIZE_PASS_BEGIN(Lint, "lint", "Statically lint-checks LLVM IR", |
| 156 | false, true) |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 157 | INITIALIZE_PASS_DEPENDENCY(AssumptionTracker) |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 158 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 159 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 160 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 161 | INITIALIZE_PASS_END(Lint, "lint", "Statically lint-checks LLVM IR", |
| 162 | false, true) |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 163 | |
| 164 | // Assert - We know that cond should be true, if not print an error message. |
| 165 | #define Assert(C, M) \ |
| 166 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
| 167 | #define Assert1(C, M, V1) \ |
| 168 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
| 169 | #define Assert2(C, M, V1, V2) \ |
| 170 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
| 171 | #define Assert3(C, M, V1, V2, V3) \ |
| 172 | do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) |
| 173 | #define Assert4(C, M, V1, V2, V3, V4) \ |
| 174 | do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) |
| 175 | |
| 176 | // Lint::run - This is the main Analysis entry point for a |
| 177 | // function. |
| 178 | // |
| 179 | bool Lint::runOnFunction(Function &F) { |
| 180 | Mod = F.getParent(); |
| 181 | AA = &getAnalysis<AliasAnalysis>(); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 182 | AT = &getAnalysis<AssumptionTracker>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 183 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 184 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 185 | DL = DLP ? &DLP->getDataLayout() : nullptr; |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 186 | TLI = &getAnalysis<TargetLibraryInfo>(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 187 | visit(F); |
| 188 | dbgs() << MessagesStr.str(); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 189 | Messages.clear(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 193 | void Lint::visitFunction(Function &F) { |
| 194 | // This isn't undefined behavior, it's just a little unusual, and it's a |
| 195 | // fairly common mistake to neglect to name a function. |
| 196 | Assert1(F.hasName() || F.hasLocalLinkage(), |
| 197 | "Unusual: Unnamed function with non-local linkage", &F); |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 198 | |
| 199 | // TODO: Check for irreducible control flow. |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void Lint::visitCallSite(CallSite CS) { |
| 203 | Instruction &I = *CS.getInstruction(); |
| 204 | Value *Callee = CS.getCalledValue(); |
| 205 | |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 206 | visitMemoryReference(I, Callee, AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 207 | 0, nullptr, MemRef::Callee); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 208 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 209 | if (Function *F = dyn_cast<Function>(findValue(Callee, /*OffsetOk=*/false))) { |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 210 | Assert1(CS.getCallingConv() == F->getCallingConv(), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 211 | "Undefined behavior: Caller and callee calling convention differ", |
| 212 | &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 214 | FunctionType *FT = F->getFunctionType(); |
Matt Arsenault | b12f2f3 | 2013-11-10 03:18:50 +0000 | [diff] [blame] | 215 | unsigned NumActualArgs = CS.arg_size(); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 216 | |
| 217 | Assert1(FT->isVarArg() ? |
| 218 | FT->getNumParams() <= NumActualArgs : |
| 219 | FT->getNumParams() == NumActualArgs, |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 220 | "Undefined behavior: Call argument count mismatches callee " |
| 221 | "argument count", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 222 | |
Dan Gohman | c128e70 | 2010-07-12 18:02:04 +0000 | [diff] [blame] | 223 | Assert1(FT->getReturnType() == I.getType(), |
| 224 | "Undefined behavior: Call return type mismatches " |
| 225 | "callee return type", &I); |
| 226 | |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 227 | // Check argument types (in case the callee was casted) and attributes. |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 228 | // TODO: Verify that caller and callee attributes are compatible. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 229 | Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end(); |
| 230 | CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 231 | for (; AI != AE; ++AI) { |
| 232 | Value *Actual = *AI; |
| 233 | if (PI != PE) { |
| 234 | Argument *Formal = PI++; |
| 235 | Assert1(Formal->getType() == Actual->getType(), |
| 236 | "Undefined behavior: Call argument type mismatches " |
| 237 | "callee parameter type", &I); |
Dan Gohman | 49a372c | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 238 | |
Dan Gohman | 3cb55a1 | 2010-12-13 22:53:18 +0000 | [diff] [blame] | 239 | // Check that noalias arguments don't alias other arguments. This is |
| 240 | // not fully precise because we don't know the sizes of the dereferenced |
| 241 | // memory regions. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 242 | if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy()) |
Dan Gohman | 7dacf8f | 2010-11-11 19:23:51 +0000 | [diff] [blame] | 243 | for (CallSite::arg_iterator BI = CS.arg_begin(); BI != AE; ++BI) |
Dan Gohman | 201acdb | 2010-12-10 20:04:06 +0000 | [diff] [blame] | 244 | if (AI != BI && (*BI)->getType()->isPointerTy()) { |
| 245 | AliasAnalysis::AliasResult Result = AA->alias(*AI, *BI); |
| 246 | Assert1(Result != AliasAnalysis::MustAlias && |
| 247 | Result != AliasAnalysis::PartialAlias, |
| 248 | "Unusual: noalias argument aliases another argument", &I); |
| 249 | } |
Dan Gohman | 49a372c | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 250 | |
| 251 | // Check that an sret argument points to valid memory. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 252 | if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 253 | Type *Ty = |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 254 | cast<PointerType>(Formal->getType())->getElementType(); |
| 255 | visitMemoryReference(I, Actual, AA->getTypeStoreSize(Ty), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 256 | DL ? DL->getABITypeAlignment(Ty) : 0, |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 257 | Ty, MemRef::Read | MemRef::Write); |
| 258 | } |
| 259 | } |
| 260 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Dan Gohman | 1249adf | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 263 | if (CS.isCall() && cast<CallInst>(CS.getInstruction())->isTailCall()) |
| 264 | for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 265 | AI != AE; ++AI) { |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 266 | Value *Obj = findValue(*AI, /*OffsetOk=*/true); |
Dan Gohman | cef9fc3 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 267 | Assert1(!isa<AllocaInst>(Obj), |
Dan Gohman | 1249adf | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 268 | "Undefined behavior: Call with \"tail\" keyword references " |
Dan Gohman | cef9fc3 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 269 | "alloca", &I); |
Dan Gohman | 1249adf | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 272 | |
| 273 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) |
| 274 | switch (II->getIntrinsicID()) { |
| 275 | default: break; |
| 276 | |
| 277 | // TODO: Check more intrinsics |
| 278 | |
| 279 | case Intrinsic::memcpy: { |
| 280 | MemCpyInst *MCI = cast<MemCpyInst>(&I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 281 | // TODO: If the size is known, use it. |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 282 | visitMemoryReference(I, MCI->getDest(), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 283 | MCI->getAlignment(), nullptr, |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 284 | MemRef::Write); |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 285 | visitMemoryReference(I, MCI->getSource(), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 286 | MCI->getAlignment(), nullptr, |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 287 | MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 288 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 289 | // Check that the memcpy arguments don't overlap. The AliasAnalysis API |
| 290 | // isn't expressive enough for what we really want to do. Known partial |
| 291 | // overlap is not distinguished from the case where nothing is known. |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 292 | uint64_t Size = 0; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 293 | if (const ConstantInt *Len = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 294 | dyn_cast<ConstantInt>(findValue(MCI->getLength(), |
| 295 | /*OffsetOk=*/false))) |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 296 | if (Len->getValue().isIntN(32)) |
| 297 | Size = Len->getValue().getZExtValue(); |
| 298 | Assert1(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) != |
| 299 | AliasAnalysis::MustAlias, |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 300 | "Undefined behavior: memcpy source and destination overlap", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | case Intrinsic::memmove: { |
| 304 | MemMoveInst *MMI = cast<MemMoveInst>(&I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 305 | // TODO: If the size is known, use it. |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 306 | visitMemoryReference(I, MMI->getDest(), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 307 | MMI->getAlignment(), nullptr, |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 308 | MemRef::Write); |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 309 | visitMemoryReference(I, MMI->getSource(), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 310 | MMI->getAlignment(), nullptr, |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 311 | MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 312 | break; |
| 313 | } |
| 314 | case Intrinsic::memset: { |
| 315 | MemSetInst *MSI = cast<MemSetInst>(&I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 316 | // TODO: If the size is known, use it. |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 317 | visitMemoryReference(I, MSI->getDest(), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 318 | MSI->getAlignment(), nullptr, |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 319 | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | |
| 323 | case Intrinsic::vastart: |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 324 | Assert1(I.getParent()->getParent()->isVarArg(), |
| 325 | "Undefined behavior: va_start called in a non-varargs function", |
| 326 | &I); |
| 327 | |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 328 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 329 | 0, nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 330 | break; |
| 331 | case Intrinsic::vacopy: |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 332 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 333 | 0, nullptr, MemRef::Write); |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 334 | visitMemoryReference(I, CS.getArgument(1), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 335 | 0, nullptr, MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 336 | break; |
| 337 | case Intrinsic::vaend: |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 338 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 339 | 0, nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 340 | break; |
Dan Gohman | a20a5cd | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 341 | |
| 342 | case Intrinsic::stackrestore: |
| 343 | // Stackrestore doesn't read or write memory, but it sets the |
| 344 | // stack pointer, which the compiler may read from or write to |
| 345 | // at any time, so check it for both readability and writeability. |
Dan Gohman | 14fe8cf2 | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 346 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 347 | 0, nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | a20a5cd | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 348 | break; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
| 352 | void Lint::visitCallInst(CallInst &I) { |
| 353 | return visitCallSite(&I); |
| 354 | } |
| 355 | |
| 356 | void Lint::visitInvokeInst(InvokeInst &I) { |
| 357 | return visitCallSite(&I); |
| 358 | } |
| 359 | |
| 360 | void Lint::visitReturnInst(ReturnInst &I) { |
| 361 | Function *F = I.getParent()->getParent(); |
| 362 | Assert1(!F->doesNotReturn(), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 363 | "Unusual: Return statement in function with noreturn attribute", |
| 364 | &I); |
Dan Gohman | ddba4b7 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 365 | |
| 366 | if (Value *V = I.getReturnValue()) { |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 367 | Value *Obj = findValue(V, /*OffsetOk=*/true); |
Dan Gohman | cef9fc3 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 368 | Assert1(!isa<AllocaInst>(Obj), |
| 369 | "Unusual: Returning alloca value", &I); |
Dan Gohman | ddba4b7 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 370 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 373 | // TODO: Check that the reference is in bounds. |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 374 | // TODO: Check readnone/readonly function attributes. |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 375 | void Lint::visitMemoryReference(Instruction &I, |
Dan Gohman | f372cf8 | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 376 | Value *Ptr, uint64_t Size, unsigned Align, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 377 | Type *Ty, unsigned Flags) { |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 378 | // If no memory is being referenced, it doesn't matter if the pointer |
| 379 | // is valid. |
| 380 | if (Size == 0) |
| 381 | return; |
| 382 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 383 | Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true); |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 384 | Assert1(!isa<ConstantPointerNull>(UnderlyingObject), |
| 385 | "Undefined behavior: Null pointer dereference", &I); |
| 386 | Assert1(!isa<UndefValue>(UnderlyingObject), |
| 387 | "Undefined behavior: Undef pointer dereference", &I); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 388 | Assert1(!isa<ConstantInt>(UnderlyingObject) || |
| 389 | !cast<ConstantInt>(UnderlyingObject)->isAllOnesValue(), |
| 390 | "Unusual: All-ones pointer dereference", &I); |
| 391 | Assert1(!isa<ConstantInt>(UnderlyingObject) || |
| 392 | !cast<ConstantInt>(UnderlyingObject)->isOne(), |
| 393 | "Unusual: Address one pointer dereference", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 394 | |
Dan Gohman | 299e7b9 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 395 | if (Flags & MemRef::Write) { |
| 396 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject)) |
| 397 | Assert1(!GV->isConstant(), |
| 398 | "Undefined behavior: Write to read-only memory", &I); |
| 399 | Assert1(!isa<Function>(UnderlyingObject) && |
| 400 | !isa<BlockAddress>(UnderlyingObject), |
| 401 | "Undefined behavior: Write to text section", &I); |
| 402 | } |
| 403 | if (Flags & MemRef::Read) { |
| 404 | Assert1(!isa<Function>(UnderlyingObject), |
| 405 | "Unusual: Load from function body", &I); |
| 406 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 407 | "Undefined behavior: Load from block address", &I); |
| 408 | } |
| 409 | if (Flags & MemRef::Callee) { |
| 410 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 411 | "Undefined behavior: Call to block address", &I); |
| 412 | } |
| 413 | if (Flags & MemRef::Branchee) { |
| 414 | Assert1(!isa<Constant>(UnderlyingObject) || |
| 415 | isa<BlockAddress>(UnderlyingObject), |
| 416 | "Undefined behavior: Branch to non-blockaddress", &I); |
| 417 | } |
| 418 | |
Duncan Sands | a221eea | 2012-09-26 07:45:36 +0000 | [diff] [blame] | 419 | // Check for buffer overflows and misalignment. |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 420 | // Only handles memory references that read/write something simple like an |
| 421 | // alloca instruction or a global variable. |
| 422 | int64_t Offset = 0; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 423 | if (Value *Base = GetPointerBaseWithConstantOffset(Ptr, Offset, DL)) { |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 424 | // OK, so the access is to a constant offset from Ptr. Check that Ptr is |
| 425 | // something we can handle and if so extract the size of this base object |
| 426 | // along with its alignment. |
| 427 | uint64_t BaseSize = AliasAnalysis::UnknownSize; |
| 428 | unsigned BaseAlign = 0; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 429 | |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 430 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Base)) { |
| 431 | Type *ATy = AI->getAllocatedType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 432 | if (DL && !AI->isArrayAllocation() && ATy->isSized()) |
| 433 | BaseSize = DL->getTypeAllocSize(ATy); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 434 | BaseAlign = AI->getAlignment(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 435 | if (DL && BaseAlign == 0 && ATy->isSized()) |
| 436 | BaseAlign = DL->getABITypeAlignment(ATy); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 437 | } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) { |
| 438 | // If the global may be defined differently in another compilation unit |
| 439 | // then don't warn about funky memory accesses. |
| 440 | if (GV->hasDefinitiveInitializer()) { |
| 441 | Type *GTy = GV->getType()->getElementType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 442 | if (DL && GTy->isSized()) |
| 443 | BaseSize = DL->getTypeAllocSize(GTy); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 444 | BaseAlign = GV->getAlignment(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 445 | if (DL && BaseAlign == 0 && GTy->isSized()) |
| 446 | BaseAlign = DL->getABITypeAlignment(GTy); |
Duncan Sands | 3f4d0b1 | 2012-09-25 10:00:49 +0000 | [diff] [blame] | 447 | } |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 448 | } |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 449 | |
| 450 | // Accesses from before the start or after the end of the object are not |
| 451 | // defined. |
| 452 | Assert1(Size == AliasAnalysis::UnknownSize || |
| 453 | BaseSize == AliasAnalysis::UnknownSize || |
| 454 | (Offset >= 0 && Offset + Size <= BaseSize), |
| 455 | "Undefined behavior: Buffer overflow", &I); |
| 456 | |
| 457 | // Accesses that say that the memory is more aligned than it is are not |
| 458 | // defined. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 459 | if (DL && Align == 0 && Ty && Ty->isSized()) |
| 460 | Align = DL->getABITypeAlignment(Ty); |
Dan Gohman | 20a2ae9 | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 461 | Assert1(!BaseAlign || Align <= MinAlign(BaseAlign, Offset), |
| 462 | "Undefined behavior: Memory reference address is misaligned", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | void Lint::visitLoadInst(LoadInst &I) { |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 467 | visitMemoryReference(I, I.getPointerOperand(), |
| 468 | AA->getTypeStoreSize(I.getType()), I.getAlignment(), |
| 469 | I.getType(), MemRef::Read); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void Lint::visitStoreInst(StoreInst &I) { |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 473 | visitMemoryReference(I, I.getPointerOperand(), |
| 474 | AA->getTypeStoreSize(I.getOperand(0)->getType()), |
| 475 | I.getAlignment(), |
| 476 | I.getOperand(0)->getType(), MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 479 | void Lint::visitXor(BinaryOperator &I) { |
| 480 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 481 | !isa<UndefValue>(I.getOperand(1)), |
| 482 | "Undefined result: xor(undef, undef)", &I); |
| 483 | } |
| 484 | |
| 485 | void Lint::visitSub(BinaryOperator &I) { |
| 486 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 487 | !isa<UndefValue>(I.getOperand(1)), |
| 488 | "Undefined result: sub(undef, undef)", &I); |
| 489 | } |
| 490 | |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 491 | void Lint::visitLShr(BinaryOperator &I) { |
| 492 | if (ConstantInt *CI = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 493 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 494 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 495 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void Lint::visitAShr(BinaryOperator &I) { |
| 499 | if (ConstantInt *CI = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 500 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 501 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 502 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | void Lint::visitShl(BinaryOperator &I) { |
| 506 | if (ConstantInt *CI = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 507 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 508 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 509 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 512 | static bool isZero(Value *V, const DataLayout *DL, DominatorTree *DT, |
| 513 | AssumptionTracker *AT) { |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 514 | // Assume undef could be zero. |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 515 | if (isa<UndefValue>(V)) |
| 516 | return true; |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 517 | |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 518 | VectorType *VecTy = dyn_cast<VectorType>(V->getType()); |
| 519 | if (!VecTy) { |
| 520 | unsigned BitWidth = V->getType()->getIntegerBitWidth(); |
| 521 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 522 | computeKnownBits(V, KnownZero, KnownOne, DL, |
| 523 | 0, AT, dyn_cast<Instruction>(V), DT); |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 524 | return KnownZero.isAllOnesValue(); |
| 525 | } |
| 526 | |
| 527 | // Per-component check doesn't work with zeroinitializer |
| 528 | Constant *C = dyn_cast<Constant>(V); |
| 529 | if (!C) |
| 530 | return false; |
| 531 | |
| 532 | if (C->isZeroValue()) |
| 533 | return true; |
| 534 | |
| 535 | // For a vector, KnownZero will only be true if all values are zero, so check |
| 536 | // this per component |
| 537 | unsigned BitWidth = VecTy->getElementType()->getIntegerBitWidth(); |
| 538 | for (unsigned I = 0, N = VecTy->getNumElements(); I != N; ++I) { |
| 539 | Constant *Elem = C->getAggregateElement(I); |
| 540 | if (isa<UndefValue>(Elem)) |
| 541 | return true; |
| 542 | |
| 543 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 544 | computeKnownBits(Elem, KnownZero, KnownOne, DL); |
Matt Arsenault | 5faa669 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 545 | if (KnownZero.isAllOnesValue()) |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | return false; |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | void Lint::visitSDiv(BinaryOperator &I) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 553 | Assert1(!isZero(I.getOperand(1), DL, DT, AT), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 554 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void Lint::visitUDiv(BinaryOperator &I) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 558 | Assert1(!isZero(I.getOperand(1), DL, DT, AT), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 559 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | void Lint::visitSRem(BinaryOperator &I) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 563 | Assert1(!isZero(I.getOperand(1), DL, DT, AT), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 564 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | void Lint::visitURem(BinaryOperator &I) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 568 | Assert1(!isZero(I.getOperand(1), DL, DT, AT), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 569 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void Lint::visitAllocaInst(AllocaInst &I) { |
| 573 | if (isa<ConstantInt>(I.getArraySize())) |
| 574 | // This isn't undefined behavior, it's just an obvious pessimization. |
| 575 | Assert1(&I.getParent()->getParent()->getEntryBlock() == I.getParent(), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 576 | "Pessimization: Static alloca outside of entry block", &I); |
Dan Gohman | 1e33b18 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 577 | |
| 578 | // TODO: Check for an unusual size (MSB set?) |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | void Lint::visitVAArgInst(VAArgInst &I) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 582 | visitMemoryReference(I, I.getOperand(0), AliasAnalysis::UnknownSize, 0, |
| 583 | nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void Lint::visitIndirectBrInst(IndirectBrInst &I) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 587 | visitMemoryReference(I, I.getAddress(), AliasAnalysis::UnknownSize, 0, |
| 588 | nullptr, MemRef::Branchee); |
Dan Gohman | d8968da | 2010-08-02 23:06:43 +0000 | [diff] [blame] | 589 | |
| 590 | Assert1(I.getNumDestinations() != 0, |
| 591 | "Undefined behavior: indirectbr with no destinations", &I); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 594 | void Lint::visitExtractElementInst(ExtractElementInst &I) { |
| 595 | if (ConstantInt *CI = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 596 | dyn_cast<ConstantInt>(findValue(I.getIndexOperand(), |
| 597 | /*OffsetOk=*/false))) |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 598 | Assert1(CI->getValue().ult(I.getVectorOperandType()->getNumElements()), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 599 | "Undefined result: extractelement index out of range", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | void Lint::visitInsertElementInst(InsertElementInst &I) { |
| 603 | if (ConstantInt *CI = |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 604 | dyn_cast<ConstantInt>(findValue(I.getOperand(2), |
| 605 | /*OffsetOk=*/false))) |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 606 | Assert1(CI->getValue().ult(I.getType()->getNumElements()), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 607 | "Undefined result: insertelement index out of range", &I); |
| 608 | } |
| 609 | |
| 610 | void Lint::visitUnreachableInst(UnreachableInst &I) { |
| 611 | // This isn't undefined behavior, it's merely suspicious. |
| 612 | Assert1(&I == I.getParent()->begin() || |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 613 | std::prev(BasicBlock::iterator(&I))->mayHaveSideEffects(), |
Dan Gohman | 9ba08a4 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 614 | "Unusual: unreachable immediately preceded by instruction without " |
| 615 | "side effects", &I); |
Dan Gohman | 7808d49 | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 618 | /// findValue - Look through bitcasts and simple memory reference patterns |
| 619 | /// to identify an equivalent, but more informative, value. If OffsetOk |
| 620 | /// is true, look through getelementptrs with non-zero offsets too. |
| 621 | /// |
| 622 | /// Most analysis passes don't require this logic, because instcombine |
| 623 | /// will simplify most of these kinds of things away. But it's a goal of |
| 624 | /// this Lint pass to be useful even on non-optimized IR. |
| 625 | Value *Lint::findValue(Value *V, bool OffsetOk) const { |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 626 | SmallPtrSet<Value *, 4> Visited; |
| 627 | return findValueImpl(V, OffsetOk, Visited); |
| 628 | } |
| 629 | |
| 630 | /// findValueImpl - Implementation helper for findValue. |
| 631 | Value *Lint::findValueImpl(Value *V, bool OffsetOk, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 632 | SmallPtrSetImpl<Value *> &Visited) const { |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 633 | // Detect self-referential values. |
| 634 | if (!Visited.insert(V)) |
| 635 | return UndefValue::get(V->getType()); |
| 636 | |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 637 | // TODO: Look through sext or zext cast, when the result is known to |
| 638 | // be interpreted as signed or unsigned, respectively. |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 639 | // TODO: Look through eliminable cast pairs. |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 640 | // TODO: Look through calls with unique return values. |
| 641 | // TODO: Look through vector insert/extract/shuffle. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 642 | V = OffsetOk ? GetUnderlyingObject(V, DL) : V->stripPointerCasts(); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 643 | if (LoadInst *L = dyn_cast<LoadInst>(V)) { |
| 644 | BasicBlock::iterator BBI = L; |
| 645 | BasicBlock *BB = L->getParent(); |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 646 | SmallPtrSet<BasicBlock *, 4> VisitedBlocks; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 647 | for (;;) { |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 648 | if (!VisitedBlocks.insert(BB)) break; |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 649 | if (Value *U = FindAvailableLoadedValue(L->getPointerOperand(), |
| 650 | BB, BBI, 6, AA)) |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 651 | return findValueImpl(U, OffsetOk, Visited); |
Dan Gohman | c575ec6 | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 652 | if (BBI != BB->begin()) break; |
| 653 | BB = BB->getUniquePredecessor(); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 654 | if (!BB) break; |
| 655 | BBI = BB->end(); |
| 656 | } |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 657 | } else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 658 | if (Value *W = PN->hasConstantValue()) |
Duncan Sands | ec7a6ec | 2010-11-17 10:23:23 +0000 | [diff] [blame] | 659 | if (W != V) |
| 660 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 661 | } else if (CastInst *CI = dyn_cast<CastInst>(V)) { |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 662 | if (CI->isNoopCast(DL)) |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 663 | return findValueImpl(CI->getOperand(0), OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 664 | } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) { |
| 665 | if (Value *W = FindInsertedValue(Ex->getAggregateOperand(), |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 666 | Ex->getIndices())) |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 667 | if (W != V) |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 668 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 669 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 670 | // Same as above, but for ConstantExpr instead of Instruction. |
| 671 | if (Instruction::isCast(CE->getOpcode())) { |
| 672 | if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()), |
| 673 | CE->getOperand(0)->getType(), |
| 674 | CE->getType(), |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 675 | DL ? DL->getIntPtrType(V->getType()) : |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 676 | Type::getInt64Ty(V->getContext()))) |
| 677 | return findValueImpl(CE->getOperand(0), OffsetOk, Visited); |
| 678 | } else if (CE->getOpcode() == Instruction::ExtractValue) { |
Jay Foad | 0091fe8 | 2011-04-13 15:22:40 +0000 | [diff] [blame] | 679 | ArrayRef<unsigned> Indices = CE->getIndices(); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 680 | if (Value *W = FindInsertedValue(CE->getOperand(0), Indices)) |
Dan Gohman | 0fa67e4 | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 681 | if (W != V) |
| 682 | return findValueImpl(W, OffsetOk, Visited); |
| 683 | } |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | // As a last resort, try SimplifyInstruction or constant folding. |
| 687 | if (Instruction *Inst = dyn_cast<Instruction>(V)) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 688 | if (Value *W = SimplifyInstruction(Inst, DL, TLI, DT, AT)) |
Duncan Sands | 64e41cf | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 689 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 690 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 691 | if (Value *W = ConstantFoldConstantExpression(CE, DL, TLI)) |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 692 | if (W != V) |
Dan Gohman | 862f034 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 693 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | 54d7aaa | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | return V; |
| 697 | } |
| 698 | |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 699 | //===----------------------------------------------------------------------===// |
| 700 | // Implement the public interfaces to this file... |
| 701 | //===----------------------------------------------------------------------===// |
| 702 | |
| 703 | FunctionPass *llvm::createLintPass() { |
| 704 | return new Lint(); |
| 705 | } |
| 706 | |
| 707 | /// lintFunction - Check a function for errors, printing messages on stderr. |
| 708 | /// |
| 709 | void llvm::lintFunction(const Function &f) { |
| 710 | Function &F = const_cast<Function&>(f); |
| 711 | assert(!F.isDeclaration() && "Cannot lint external functions"); |
| 712 | |
| 713 | FunctionPassManager FPM(F.getParent()); |
| 714 | Lint *V = new Lint(); |
| 715 | FPM.add(V); |
| 716 | FPM.run(F); |
| 717 | } |
| 718 | |
| 719 | /// lintModule - Check a module for errors, printing messages on stderr. |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 720 | /// |
Dan Gohman | 084bcb1 | 2010-05-26 22:28:53 +0000 | [diff] [blame] | 721 | void llvm::lintModule(const Module &M) { |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 722 | PassManager PM; |
| 723 | Lint *V = new Lint(); |
| 724 | PM.add(V); |
| 725 | PM.run(const_cast<Module&>(M)); |
Dan Gohman | 98bc437 | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 726 | } |